mysql根据列

时间:2015-04-22 23:51:33

标签: mysql

您好我有以下专栏

| id | name | category|
|  1 | sam  | doctor  |
|  2 | tom  | doctor  |
|  3 | pam  | nurse   |
|  4 | gum  | nurse   |
|  5 | tom  | doctor  |
|  6 | lim  | doctor  |

我想运行一个查询,根据name列唯一的类别选择不同的名称

| id | name | category|
|  1 | sam  | doctor  |
|  2 | tom  | doctor  |
|  3 | pam  | nurse   |
|  4 | gum  | nurse   |
|  6 | lim  | nurse   |

让我感到困惑

1 个答案:

答案 0 :(得分:3)

您可以使用min

select min(id), name, category
from yourtable
group by name, category

如果你真的只想要不同的名字,那么既然你正在使用mysql,那么这也会有效(但返回随机ID和类别)。如果您需要特定的ID /类别,则需要在聚合中定义它们(如上一个解决方案中所述):

select id, name, category
from yourtable
group by name
相关问题