说我有这样一张桌子:
model_id | object_id | date_modified
1 | 123 | NULL
1 | 123 | 2012-12-11 16:55:21
1 | 456 | 2012-12-11 16:52:21
1 | 789 | NULL
我想返回尚未修改的object_ids。这是我到目前为止所尝试的内容:
SELECT object_id from Table WHERE date_modified IS NULL
这会返回123和789,我只想要789。
SELECT DISTINCT object_id from Table WHERE date_modified IS NULL
这也会返回123和789,因为在WHERE过滤结果后应用了DISTINCT子句。
有没有办法通过单个查询实现这一点,还是应该返回一个不同的object_id列表并循环并以编程方式过滤?
答案 0 :(得分:2)
SELECT object_id from Table GROUP BY object_id HAVING MAX(date_modified) IS NULL
答案 1 :(得分:0)
嵌套查询会有所帮助。内部查询选择已修改的所有object_id,然后外部查询选择具有空日期但object_id不在修改列表中的所有记录。
SELECT object_id FROM table
WHERE date_modified IS NULL AND object_id NOT IN (
SELECT object_id FROM table
WHERE date_modified IS NOT NULL
)
答案 2 :(得分:0)
查询:
SELECT DISTINCT t.object_id
from Table1 t
WHERE t.object_id NOT IN (SELECT t1.object_id
FROM Table1 t1
WHERE t1.date_modified IS NOT NULL)
结果:
| OBJECT_ID |
-------------
| 789 |