PHP,MySQL,列出两个表之间没有链接的记录?

OK, I inherited a truly messed up database and have been tasked with cleaning it up.

The problem is this… I have to compare 2 tables. table1 (members) and table2 (associations) and find all instances of members that no longer have associations, and where associations no longer point to a member

The association table, table2, has a field called mbr_id that, as you can guess, links it to a member’s id field in table1.

I am assuming I will need some sort of while loop…but I how no idea how to form the query. Something like…

SELECT * FROM `members2`, `assocs` WHERE `assocs.mbr_id` != `members2.id`

Is that it? Can’t be… right?

That can't be right is correct. Try these two queries:

select m.*
from members m
where not exists (select 1 from assocs a where a.mbr_id = m.id);

and:

select a.*
from assocs a
where not exists (select 1 from members m where a.mbr_id = m.id);

The first is members with no valid associations. The second are associations with no valid members.