从表中获取多个ID的最后插入数据?

时间:2014-09-17 07:39:56

标签: php mysql sql

我有一个表"event_activities",它记录了所有事件的活动。

它有一个字段' event_id' 'id'表中的"events"。现在我想检索每个事件的最后插入行,事件应该是不同的。

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


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

我已尝试使用DISTINCT,但我还需要更多字段,我也尝试使用group by,但未成功。

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;

两个查询都没有给我所需的结果。 有人可以带来一个完成我工作的查询。

1 个答案:

答案 0 :(得分:2)

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