我有一个表,它记录了所有事件的活动。
它有一个字段'event_id',在"event_activities"表中是'id'。现在我要检索每个事件的最后一个插入行,并且事件应该是不同的。

event_activities
------------------
id eventid date


events
----------
id name place

我试过使用"events"但是我还需要更多的字段,我也试过使用DISTINCT但是没有成功。
select distinct a.id, a.eventid, a.date, b.name from event_activities as a join events as b on a.eventid = b.id;


select a.id, a.eventid, a.date, b.name from event_activities as a join events as b on a.eventid = b.id order by a.eventid;

两个查询都没有给出所需的结果。
有人能提出一个能帮我解决问题的问题吗。

最佳答案

select a.id, a.eventid, a.date, b.name
from event_activities as a join events as b on a.eventid = b.id
where a.date = (select max(date) from event_activities a2
                where a2.eventid = a.eventid)
order by a.eventid

关于php - 从表中获取最后插入的多个ID的数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25884922/

10-12 02:47