Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select distinct and one another column of the id

I have a table with multiple columns but I need only 2.

select id, department from tbl

If I want to use distinct, how do I do that? This is not working:

select id, distinct department from tbl
like image 372
Y.G.J Avatar asked Jan 21 '23 03:01

Y.G.J


2 Answers

Use the following to get distinct rows:

select distinct id, department 
from tbl

However, you can't simply get distinct departments if some departments have multiple Id's - you need to figure out which of the multiple Id's you want (max? min? something else?).

like image 141
Oded Avatar answered Feb 01 '23 01:02

Oded


SELECT  * FROM Table c1
 WHERE ID = (SELECT MIN(ID) FROM Table c2
    WHERE c1.department = c2.department)
like image 31
jwize Avatar answered Feb 01 '23 00:02

jwize