我需要使用php比较两个mysql记录集
比较第一个记录集中存在于第二个记录集中的记录并返回真值,如果不存在则返回假值。
问题是,一旦做记录集的单次传递,它不会返回到开始,所以我可以比较recordset1的剩余记录。
我使用的代码是:
*mysql_select_db($database_db, $db);
$query_rec_teams = "SELECT tbl_items.* FROM tbl_items;";
$rec_items = mysql_query($query_rec_items, $db) or die(mysql_error());
$row_rec_items = mysql_fetch_assoc($rec_items);
$totalRows_rec_items = mysql_num_rows($rec_items);
$query_rec_itemsLeft = "(SELECT tbl_itemsleft.* FROM tbl_itemsLeft";
$rec_itemsLeft = mysql_query($query_rec_itemsLeft, $db) or die(mysql_error());
$row_rec_itemsLeft = mysql_fetch_assoc($rec_itemsLeft);
$totalRows_rec_itemsLeft = mysql_num_rows($rec_itemsLeft);
//iterate first recordset, populate variables
do{
$itemPresent=0;
$item=$row_rec_item['ItemID'];
//iterate second recordset and compare item variable with itemID fields in recordset, if exist echo true, else echo false
do {
if ($row_rec_itemsLeft(['ItemID'] == $item){
itemPresent=1;
}
else{
itemPresent=0;
}
echo itemPresent;
} while ($row_rec_itemsLeft = mysql_fetch_assoc($rec_itemsLeft));
} while ($row_rec_items = mysql_fetch_assoc($rec_items));
mysql_free_result($rec_itemsLeft);
mysql_free_result($rec_items);
?>*
将记录集填充到数组中并比较数组是否更容易呢?
帮助欣赏
使用MYSQL JOIN来检索结果
在join中引用这个
http://www.sitepoint.com/understanding-sql-joins-mysql-database/您应该使用MYSQL join来完成此操作。如果您只想显示两者都包含的项:
SELECT *
FROM tbl_items
INNER JOIN tbl_itemsLeft
ON tbl_items.ItemID=tbl_itemsLeft.ItemID
如果你想查看tbl_items中的所有项目,并查看任何与tbl_itemsLeft匹配或不匹配的项目,那么你可以执行LEFT JOIN,任何tbl_itemsLeft为NULL的记录将是tbl_itemsLeft表中没有匹配的记录。
SELECT *
FROM tbl_items
LEFT JOIN tbl_itemsLeft
ON tbl_items.ItemID=tbl_itemsLeft.ItemID
另一个提示是你不应该在php中使用mysql api函数。这些都是PHP 5.5.0中弃用的函数,您的代码将在未来的PHP升级后不再工作。我建议您使用PDO