我有这两个表:
Organiser Events
---------- -------
id 1 id
|__ * organiser_id
date
status
如何选择所有组织者的ID,这些组织者的最新事件的状态等于“活跃”。
我想我必须在子查询中使用max(date)或者我是否需要使用连接?仍然没有找到解决方案,所以任何帮助表示赞赏!
由于
编辑:
我刚刚找到了这个解决方案(dunno,如果这是最好的?):
SELECT o.id, e.date
FROM Organiser o
INNER JOIN events e on e.organiser_id = o.id
WHERE e.date = (SELECT max(date)
FROM events e2
WHERE e2.organiser_id = o.id)
答案 0 :(得分:1)
试
SELECT o.name, ep.date
FROM ( SELECT date,max(id) AS maxDateID FROM events es GROUP BY date ) md
INNER JOIN events ep ON ep.id=md.id
INNER JOIN organiser o ON o.id=ep.organiser_id
WHERE e.status='active'
答案 1 :(得分:1)
SELECT
id
FROM
Organiser AS o
WHERE
( SELECT status
FROM Events AS e
WHERE e.organiser_id = o.id
ORDER BY `date` DESC
LIMT 1
) = 'active'