简体   繁体   English

如何在MySQL的另一个表中仅选择没有相应外键的行?

[英]How do I select only rows that don't have a corresponding foreign key in another table in MySQL?

I have 3 tables: 我有3张桌子:

  • listing 清单
  • photo 照片
  • calendar 日历

"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.

相关问题 如何将通过外键连接的两个表中的所有行迭代到 PHP、CodeIgniter、MySQL 中的单个表中 - How do I iterate all rows from two tables connected by foreign key into a single table in PHP, CodeIgniter, MySQL 如何为表中的新行创建外键? -MySQL-CodeIgniter - How do you make a foreign key for a new row in a table? - mysql - CodeIgniter Mysql - 如何根据我是否从另一个表中订阅它们来从一个表中选择条目? - Mysql - How to select entries from one table based on whether or not I subscribe to them from another table? 我想按条件删除数据。如果数据在另一个表中作为外键可用,则不要删除它 - I want to delete data on condition.if data is available in another table as a foreign key then donot delete this Codeigniter:当$ query-> num_rows()对我不起作用时,如何选择计数? - Codeigniter: how do I select count when `$query->num_rows()` doesn't work for me? 选择一个表中不匹配的行(mysql) - Select rows does not match in one table(mysql) 我如何在MySQL中更新表A联接另一个表? - How can I UPDATE Table A in MySQL joining another table? 如何从表中获取数据,将日期作为键和相应的数据? - How to get data from table, making date as key and corresponding data? 如何移动div,以免它们重叠 - How do I move div so they don't overlap 我如何回显具有特定 ID 的行 - How do i echo rows that have a specific id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM