mysql - select distinct records where id's are different

other
voted: 0
vote
mysql select distinct names

mysql - select distinct records where id's are different. Lets say that we hawe workers table:

create table workers (id int not null auto_increment, name varchar(20),primary key (id));

INSERT INTO workers (name) VALUES ('Jamal'), ('Bob'), ('Bob'), ('Jena'),('Luke'),('Mike'),('Mike')

So we have table with following data:

id 	name
1 	Jamal
2 	Bob
3 	Bob
4 	Jena
5 	Luke
6 	Mike
7 	Mike

Now if we want query records with distinct names we use query:

SELECT * FROM (SELECT name,MAX(id) id FROM `workers` GROUP BY name) workers_unique_by_names

and we have results:

id 	name
3 	Bob
1 	Jamal
4 	Jena
5 	Luke
7 	Mike

If we want only name column

we use query:

SELECT name FROM (SELECT name,MAX(id) id FROM `workers` GROUP BY name) workers_unique_by_names

and we have results:

name
Bob
Jamal
Jena
Luke
Mike
working url: not entered
version: not entered
Leave a Comment:
Characters left:
No comments yet.