开发者

Find rows without relations in sqlite3?

开发者 https://www.devze.com 2023-04-06 22:36 出处:网络
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 h

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消