简体   繁体   中英

How to retrive the last inserted id using 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 

I am trying to get the last inserted records based on the event_id this query brings back all the records.

similar to what mysql_insert_id() would do.

Is it possible to modify the query to return only the last inserted record?

my data looks like this I need to return the last event_id and its matching res_id

event_id  | res_id
116       | 1
116       | 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM