Reputation: 994
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 picture
but it is showing something else, it showing this output
here the mysql code I tried SELECT *,max(id) FROM activity WHERE visitorid=1
Upvotes: 0
Views: 367
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
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