Shane
Shane

Reputation: 753

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()); 

Upvotes: 0

Views: 50

Answers (2)

Jahid Mahmud
Jahid Mahmud

Reputation: 1136

You should write this

$result11=mysql_query("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")or die('Error' . mysql_error()); 

Upvotes: 0

Akin Williams
Akin Williams

Reputation: 658

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;

Upvotes: 1

Related Questions