user3092953 user3092953 - 4 months ago 15
SQL Question

Select from table where doesn't appear in another table PHP

I have 2 tables, one called

system_dashboard_stats
(tbl1) and another called
user_dashboard_stats
(tbl2). I want to display all rows from tbl1 that dont appear in tbl2.

So far, from looking i've found the below code, however it doesnt seem to be working as I currently see all rows from the DB.

$result11=mysql_query("SELECT * FROM system_dashboard_stats WHERE id NOT IN (SELECT id FROM user_dashboard_stats)")or die('Error' . mysql_error());

Answer

SELECT * FROM system_dashboard_stats LEFT JOIN user_dashboard_stats ON system_dashboard_stats.pk = user_dashboard_stats.fk WHERE user_dashboard_stats.fk IS NULL;

Comments