我对SQL非常陌生,所以我真的不知道我在做什么!
我想查询显示每个客户最近的评论,所以经过研究;我发现了以下问题:
Access SQL Query: Find the most recent date entry for each employee for each training course
然后我通过命名我的表格和字段来修改答案以适合我的场景 - 给我这个:
SELECT t1.*, t2.*
FROM
Clients t1, Reviews t2
INNER JOIN
(
SELECT Reviews.[Review ID], Clients.[Client ID], Clients.[Title], Clients.[First name], Clients.[Surname, MAX(Reviews.[Review date]) AS MaxDate
**FROM** Reviews, Clients
GROUP BY Reviews.[Review ID], Clients.[Client ID]
) t3
ON t1.[Client ID]=t3.[Client ID]
AND t2.[Review ID]=t3.[Review ID]
AND t2.[Review date]=t3.MaxDate
但是当我运行它时,我在FROM子句中遇到语法错误。我加粗了这个条款。 对于我的查询,我想要显示:
Review ID....Client ID....Title....First name....Surname....Review date
1 1 Mr Gerry Parker 05/10/2015
3 2 Mrs Georgina Phillips 22/04/2016
当实际存在Georgina Phillips的旧评论时,查询应仅显示所有客户的最新或最新评论。我只需要查看日期,而不是实际的评论数据。
每个客户都可以有多个评论,但每个评论只能有一个客户。
答案 0 :(得分:0)
我怀疑你想要这样的东西:
SELECT c.*, r.*
FROM Clients as c INNER JOIN
Reviews as r
ON c.[Client Id] = r.[Client Id]
WHERE r.[Review Date] = (SELECT MAX(r2.[Review Date])
FROM Reviews as r2
WHERE r2.[Client Id] = r.[Client Id]
);