How to retrive the last inserted id using mysql?


Problem :

 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


Solution :

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)

Mysql Tutorials

Mysql Howto..

how to get multiple items in one line in mysql?

How can I use multidimensional arrays in PHP with MySQL-based sessions?

How to shuffle the results of two MySQL SELECT statements

MySQL - how to sum counts by group

How to combine this two MySQL query using Group By

How can I get this MySql WordPress query to run faster?

SQL: How to convert REPLACE INTO … SELECT … FROM query from MySQL to SQL server?

How to create a table with php variable as table name?

How to get the column name of the result of a least function?

How do I search datas in different database tables? [closed]

How to improve security of mysql database

How to Call Java Code from MySQL?

MYSQL - How to return array from first table and select from another table using that array in one query?

How to connect to a database created in using ANT

How to fetch only a value from MySQL database? [duplicate]

how to replicate the scenario of master failure manually in mysql [closed]

how to enable Direct Database Access while creating database in mysql? [closed]

How to get mysql back up tables starting with specific letter?

How to make a custom column's value in mysql query

How to remove “similar” but not identical content in a MySQL database

mysql how to show some rows of table depending on one field and depending on one field in other table?

how to prevent anyone from dropping, deleting, and changing the contents of a log table in mysql

How to compare EST date time with mysql data

how to fetch only a row from a mysql query

How to change this MySQL query to SELECT from one table instead of two?

How to get data form mysql database using php date?

how to get single value from comma separated array when fetching data from MYSQL

How to select/match from MySQL table?

How Encoding problems are solves in the web [closed]

How to delete records in mysql between databases