Reputation: 31225
I have a table with the following. The names are not unique.
userid, name
1, dave
2, john
3, mike
4, mike
5, dave
I want to return the unique name with the highest userid.
ie.
2, john
4, mike
5, dave
What is the query to do so?
Upvotes: 0
Views: 98
Reputation: 61331
select userid, name from users where name='john' order by userid desc limit 1
or
select userid, name from users where userid
= (select max(userid) from users where name='john' )
The first syntax is MySQL-specific (no "limit" clause in other databases).
Upvotes: -2
Reputation: 22895
select name, max(userid) as max_userid
from users
group by name
order by max(userid) asc
Upvotes: 6