[英]MySQL Join table and get results that don't exist in one
I have two tables, one that has a foreign key from the other. 我有两个表,其中一个具有另一个的外键。 I want to get all records that don't exist in the foreign table, based on certain criteria.
我想根据某些条件获取外部表中不存在的所有记录。
Here are the tables I have: 这是我的表格:
item_setting item_setting
item 项目
Here's the query I'm using now: 这是我现在正在使用的查询:
SELECT
iset.setting_id
FROM
item_settings iset
LEFT OUTER JOIN
item i ON i.setting_id = iset.setting_id
WHERE
iset.category_id = '5' AND i.setting_id is null
This query works in providing any setting_id's that do not have a record in the item's table within a specific category. 此查询的作用是提供特定类别中项目表中没有记录的任何setting_id。
However, now I want to include cases where the expired_dt less than than time() (meaning it's past expired). 但是,现在我想包括expired_dt小于time()的情况(这意味着它已经过期)。 In otherwords, I would think to add this:
换句话说,我想补充一下:
WHERE
iset.category_id = '5' AND (i.setting_id is null OR i.expired_dt < '".time()."')
However, this doesn't work, it returns all the records. 但是,这不起作用,它将返回所有记录。
Any suggestions? 有什么建议么? Maybe I'm completely over complicating this.... I just want to return the setting_id's from the item_settings table, where the expired_dt associated in the item table is expired or if it does not even exist in the item table.
也许我已经完全结束了此事。...我只想从item_settings表中返回setting_id,其中item表中关联的expired_dt已过期,或者甚至在item表中不存在。
Thank you! 谢谢!
Try moving the timestamp condition into the join clause. 尝试将时间戳记条件移到join子句中。 Something like
就像是
item_settings iset
LEFT OUTER JOIN
item i ON i.setting_id = iset.setting_id and i.expired_dt > time()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.