Hi I have the 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 to run a query to choose distinct names based on category where name
column is unique to get below
| id | name | category|
| 1 | sam | doctor |
| 2 | tom | doctor |
| 3 | pam | nurse |
| 4 | gum | nurse |
| 6 | lim | nurse |
It baffles me
You could use min
:
select min(id), name, category
from yourtable
group by name, category
If you truly just want distinct names, then since you're using mysql, this would work as well (but return random ids and categories). If you need specific ids/categories, you'll need to define them in an aggregate (as in the previous solution):
select id, name, category
from yourtable
group by name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.