zadubz
zadubz

Reputation: 1301

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

Upvotes: 0

Views: 82

Answers (1)

Aaron W.
Aaron W.

Reputation: 9299

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)

Upvotes: 2

Related Questions