There is 2 queries. One recordset with all the ID's (e.g. 1,2,3,4) and the other with ID's not being available (e.g. 1,2,2,2,4,4).
How would I go about producing all records not contained in the 2nd recordset (e.g. 3)?
(array_diff_assoc doesn't work as my version of PHP is below 4.3.0 if that approach even makes sense...)
Thanks for any input,
sv
array_diff with mySQL problem
do you have array_diff() :
$a = array(1,2,3,4);
$b = array(1,1,2,2,2,4,4);
$tmp = array_diff($a,$b);
print_r($tmp);
Thank you!
This works for me if I define the arrays like in your example. But I guess my problem is how to define the arrays from my recordsets.
Could you post an example including the mysql_fetch... part?
You can find unmatched keys in two recordsets with a left join as shown below. If you particularly need arrays I've shown that too.
hth
$cnx = mysql_connect("localhost") or die("Not connected");
mysql_select_db("test",$cnx);
/*
mysql_query("create table tablea (id int not null auto_increment primary key, description varchar(5))");
mysql_query("create table tableb (id int not null )");
mysql_query("insert into tablea (description) values ('AAAAA')" );
mysql_query("insert into tablea (description) values ('BBBBB')" );
mysql_query("insert into tablea (description) values ('CCCCC')" );
mysql_query("insert into tablea (description) values ('DDDDD')" );
mysql_query("insert into tableb (id) values (1)" );
mysql_query("insert into tableb (id) values (1)" );
mysql_query("insert into tableb (id) values (2)" );
mysql_query("insert into tableb (id) values (2)" );
mysql_query("insert into tableb (id) values (2)" );
mysql_query("insert into tableb (id) values (4)" );
mysql_query("insert into tableb (id) values (4)" );
mysql_query("insert into tableb (id) values (4)" );
mysql_query("insert into tableb (id) values (4)" );
*/
/* to find unmatched records using sql only*/
$sql = "select a.id, a.description from tablea a left join tableb b on a.id = b.id where b.id is null";
$result = mysql_query ($sql);
while (list($id,$descrip) = mysql_fetch_row($result)) {
echo "$id $descrip was unmatched<br>";
}
/* alternatively, using arrays */
$result = mysql_query("select id from tablea");
while ($row = mysql_fetch_row($result)) {
$a[] = $row[0];
}
$result = mysql_query("select id from tableb");
while ($row = mysql_fetch_row($result)) {
$b[] = $row[0];
}
$tmp = array_diff($a,$b);
echo "a "; print_r($a); echo "<br>";
echo "b "; print_r($b); echo "<br>";
echo "tmp "; print_r($tmp); echo "<br>";
You made it work!
Using sql only conflicts with other conditions on my recordsets but using your array example works perfectly!
Thank you!