[英]How do I select only rows that don't have a corresponding foreign key in another table in MySQL?
I have 3 tables: 我有3张桌子:
"photo" and "calendar" both have a "listing_id" column in them and every "listing" has a "photo". “照片”和“日历”中都有一个“ listing_id”列,每个“列表”都有一个“照片”。 But I only want to select rows that have no entry in the "calendar" table with the matching "listing_id". 但是我只想选择在“ calendar”表中没有匹配项“ listing_id”的行。
I'm not sure if I'm saying it correctly, but any help would be greatly appreciated. 我不确定我说的是否正确,但是任何帮助将不胜感激。 And if someone could show me CodeIgniter syntax, that'd be even better. 如果有人可以告诉我CodeIgniter语法,那就更好了。
This will produce the list of calendar.free_date values that should not be returned because their associated listing_id values do not exist in the listing table. 这将产生calendar.free_date值的列表,由于列表表中不存在与它们关联的listing_id值,因此不应返回这些值。
select free_date from calendar c
where not exists (select * from listing
where listing_id = c.listing_id);
Should work as an SQL query. 应作为SQL查询工作。 Not sure about CI syntax. 不确定CI语法。 Sorry! 抱歉!
SELECT * FROM listing WHERE listing_id NOT IN (SELECT listing_id FROM calendar) SELECT * FROM清单WHERE listing_id不在(SELECT清单FROM日历)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.