7

这看起来很简单,但如果不做子查询我似乎无法弄清楚(这似乎显着减慢了查询速度 - 需要将近 10 秒而不是 <1)。

假设我有一张已发送文档的表格,我想选择自上次发送以来已更新的文档,以及从未发送过的文档。

SELECT d.document_id, max(sd.document_sent_date) as last_sent_date
FROM documents d
LEFT JOIN sent_documents sd ON d.document_id=sd.document_id
WHERE last_sent_date is NULL OR last_sent_date<d.last_updated
GROUP BY d.document_id

这样的事情可能吗?基本上,我想在 where 子句中使用 max() 的结果。

4

1 回答 1

9

你想要这个having子句。

一般规则:在聚合where对日期进行操作,对聚合的数据进行操作。having

于 2013-02-01T02:10:46.820 回答