How to compare two tables and return mismatches with PHP

How to compare two tables and return mismatches with PHP



I have 2 tables below with both a list of names.



table1


id name joined_on
1 Daniel Smith 2018-07-01 00:00:00
2 James Owen 2018-07-03 00:00:00
3 Dave John 2018-04-04 00:00:00
4 Dean Davidson 2018-02-01 00:00:00
5 James Saunders 2018-07-04 01:05:02
6 Earl Smith 2018-07-04 01:05:19
7 Faud Earl 2018-07-04 01:07:46
8 Casper James 2018-05-01 00:00:00



table2


id name joined_on
1 Daniel Smith 2018-07-04 00:00:00
2 James Owen 2018-07-04 01:04:03
3 Dale Davidson 2018-02-02 00:00:00
4 Faud Earl 2018-05-15 00:00:00
5 Casper James 2018-05-26 00:00:00
6 Dave John 2018-07-04 01:05:10



How do I compare all names of table1 with all the names of table2 and return all mismatches. I want to achieve that it will return all names from table1 that are not in table2.



I need this for a school assignment but I just don't know where to start. I'd appreciate if somebody could help.



Edit:



Now I got this, I tried printing the result out on different ways but it doesn't return the names, it only returns "NULL".


$sql = "SELECT name from Players_christmas where name not in (select name from Players_halloween";
$assoc = mysqli_fetch_assoc($sql);
var_dump($assoc);





something like ...WHERE name not in (Select name from table2)
– Jeff
Aug 23 at 19:36


...WHERE name not in (Select name from table2)





after EDIT: if this is your full code, then you are missing a mysqli_query($databaseConnection, $sql) before you fetch (from result!). Please refer to the manual for examples.
– Jeff
Aug 23 at 21:13


mysqli_query($databaseConnection, $sql)




3 Answers
3



You can do directly in SQL



You could use left join and check for null values


select name from table1
left join table2 on table1.name = table2.name
where t2.name is null



I'm thinking you could query to get all data from each table and for each row, store as an associative array or just a regular array into an overall array as shown below.


$sql = "SELECT * FROM your_table1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
$table1_rows[$i] = $row;
$i++;



then once you have both $table1_rows and $table2_rows, you can use this difference between arrays function ( array_diff_assoc for associative arrays, array_diff for a standard array )



$array_of_different_indexes = array_diff($table1_rows,$table2_rows);


$array_of_different_indexes = array_diff($table1_rows,$table2_rows);



the array_diff function is really handy, here's a link for it
https://secure.php.net/manual/en/function.array-diff.php





Functional, but probably better to handle at the database level as this could get really messy for large numbers of records.
– tadman
Aug 23 at 19:49



As you already have in your description "all names from table1 that are not in table2" you can do:


SELECT `name` from `table1`
WHERE `name` not in (SELECT `name` from `table2`)



here's a fiddle: http://sqlfiddle.com/#!9/e87c78/1





Hey, I tried putting this into a PHP code because I want to display it on a webpage but it just returns "NULL" and not the names. I edited my post with what I have now.
– JF058
Aug 23 at 20:40





after EDIT: if this is your full code, then you are missing a mysqli_query($databaseConnection, $sql) before you fetch (from result!). Please refer to the manual for examples.
– Jeff
Aug 23 at 21:13


mysqli_query($databaseConnection, $sql)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)