使用php比较两个mysql记录集

I need to compare two mysql recordsets using php

Comparing a record in the first recordset is present in the second and returns a true value, or a false if its not present.

The problem is once a do a single pass of the recordset it doesn't return to the beginning so i can compare the the remaining records of recordset1 to it.

The code i'm using is:

*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);
?>*

would it be easier to populate the recordsets into arrays and compare the arrays instead

help appreciated

Use MYSQL JOIN for retrieving the results.

Refer this one for JOINS..

http://www.sitepoint.com/understanding-sql-joins-mysql-database/

You should use MYSQL Joins to accomplish this. If you only want to show items that are in both:

SELECT * 
FROM tbl_items
     INNER JOIN tbl_itemsLeft 
     ON tbl_items.ItemID=tbl_itemsLeft.ItemID

If you want to view all items from tbl_items and see any that match or do not match with tbl_itemsLeft then you can do a LEFT JOIN and anything that has tbl_itemsLeft IS NULL will be records that do not have a match in the tbl_itemsLeft table.

SELECT * 
FROM tbl_items 
     LEFT JOIN tbl_itemsLeft 
     ON tbl_items.ItemID=tbl_itemsLeft.ItemID

Another tip is that you should not use mysql api functions in php. These are deprecated functions as of PHP 5.5.0 and your code will no longer work after future php upgrades. I would recommend you look at running your queries using PDO