mysql select distinct column based on group by column -
hi have following columns
| id | name | category| | 1 | sam | doctor | | 2 | tom | doctor | | 3 | pam | nurse | | 4 | gum | nurse | | 5 | tom | doctor | | 6 | lim | doctor |
i want run query choose distinct names based on category name
column unique below
| id | name | category| | 1 | sam | doctor | | 2 | tom | doctor | | 3 | pam | nurse | | 4 | gum | nurse | | 6 | lim | nurse |
it baffles me
you use min
:
select min(id), name, category yourtable group name, category
if want distinct names, since you're using mysql, work (but return random ids , categories). if need specific ids/categories, you'll need define them in aggregate (as in previous solution):
select id, name, category yourtable group name