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?
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?
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).
select name, max(userid) as max_userid
from users
group by name
order by max(userid) asc