如何使用mysql检索最后插入的id?

时间:2012-08-03 00:25:07

标签: php mysql

 SELECT event.event_name, restaurants.res_name 
 FROM event_choices
 INNER JOIN restaurants
 ON event_choices.res_id = restaurants.res_id
 INNER JOIN event
 ON event_choices.event_id = event.event_id 

我正在尝试根据event_id获取最后插入的记录,此查询会带回所有记录。

类似于mysql_insert_id()会做的事情。

是否可以修改查询以仅返回最后插入的记录?

我的数据看起来像这样我需要返回上一个event_id及其匹配的res_id

event_id  | res_id
116       | 1
116       | 2

1 个答案:

答案 0 :(得分:2)

SELECT event.event_name, restaurants.res_name 
FROM event_choices
INNER JOIN restaurants ON event_choices.res_id = restaurants.res_id
INNER JOIN event ON event_choices.event_id = event.event_id 
WHERE event.event_id = (SELECT MAX(event_id) FROM event)