Amit Shakya
Amit Shakya

Reputation: 994

how to select row in mysql with max id

Hi I am trying to select row in mysql whose visitor id=1 and max id then it should be the last row as I focus in this pictureenter image description here

but it is showing something else, it showing this output enter image description here

here the mysql code I tried SELECT *,max(id) FROM activity WHERE visitorid=1

Upvotes: 0

Views: 367

Answers (2)

chresse
chresse

Reputation: 5817

you can do it with:

SELECT * FROM activity WHERE visitorid = 1 ORDER BY id DESC LIMIT 1

this will order your rows (where visitorid = 1) descending by id and select only the first one.

Upvotes: 1

Ravinder Reddy
Ravinder Reddy

Reputation: 24022

When there exists multiple entries for an id in a table
and you still want to fetch the latest, then
filter the records in descending order of auto incremented primary key and limit to top record.

Example:

select * from activity 
 where visitorid = 1
 order by id desc
 limit 1

Upvotes: 0

Related Questions