Find rows without relations in sqlite3?
I have tw开发者_开发问答o tables. One called "members" and another called "homes" (should be household, but I suck in english). Those have a many-one relation (i.e. several members belong to one household). Those are linked together by members.homefk and homes.Id
Now, how can I find homes that don't belong to any members? I want this for house cleaning purposes.
SELECT homes.*
FROM homes
LEFT JOIN members ON (members.home_id = home.id)
WHERE members.home_id IS NULL
Use a subquery to return all the homefk values, then select from homes where id not in the subquery,
In Oracle would look something like
SELECT h.id FROM homes h WHERE h.id NOT IN( SELECT DISTINCT(m.homefk) FROM members m)
精彩评论