使用首选的显式 JOIN 表示法以及 GROUP BY 和 HAVING 子句,您应该能够使用:
SELECT i.itemid, i.name, i.price
FROM Item i
JOIN ItemComment c ON i.itemid = c.itemid
GROUP BY i.itemid, i.name, i.price
HAVING MAX(c.dateCommentPosted) < (ADD_MONTHS(SYSDATE, -6));
您还可以使用 WITH 子句或直接在 FROM 列表中编写子查询,以计算为每个项目发布评论的最近日期,然后将其与 Item 表连接。
您可能需要考虑是否应选择没有任何注释的项目。
如果我也想显示/选择怎么办Max(c.dateCommentPosted)
。这是一个选择吗?如果我在 select 子句中添加它并dateCommentPosted
在 GROUP BY 子句中添加,那是否正确?
将其添加到 select 子句,但不是 GROUP BY 子句:
SELECT i.itemid, i.name, i.price, MAX(c.dateCommentPosted) AS dateCommentPosted
FROM Item i
JOIN ItemComment c ON i.itemid = c.itemid
GROUP BY i.itemid, i.name, i.price
HAVING MAX(c.dateCommentPosted) < (ADD_MONTHS(SYSDATE, -6));
您可能更愿意将 AS 排除在外。AFAIK,Oracle 绝对拒绝表别名中的 AS(所以我把它留在那里),但它在选择列表的“列别名”中接受(但不需要)它。
Incidentally, the SQL Standard, and therefore 'all' SQL DBMS, require aggregate comparisons in the HAVING clause, and the HAVING clause requires a GROUP BY clause. Hence, GBH — Group By / Having as well as Grievous Bodily Harm.