仅按列的特定值分组

时间:2016-06-28 07:13:53

标签: php mysql

考虑下表,例如

+----+-------+--------------+
| ID |  TYPE |     NAME     |
+----+-------+--------------+
| 1  | 13    | Free color   |
| 2  | 15    | Free food    |
| 3  | 15    | Free burger  |
| 4  | 17    | Free cloths  |
| 5  | 13    | Free crayons |
| 6  | 12    | Free wood    |
+----+-------+--------------+

我正在尝试构建一个查询,group by只会在group by列上使用普通type来键入15,它会按所有类型进行分组,甚至13型,我不想要。我只想按特定值的列类型进行分组(此处为15)。在MySQL中有可能吗?如果是这样,怎么样?

预期结果如下所示:

+----+-------+--------------+
| ID |  TYPE |     NAME     |
+----+-------+--------------+
| 1  | 13    | Free color   |
| 2  | 15    | Free food    |
| 4  | 17    | Free cloths  |
| 5  | 13    | Free crayons |
| 6  | 12    | Free wood    |
+----+-------+--------------+

2 个答案:

答案 0 :(得分:2)

你可以这样做:

ActivityManager

答案 1 :(得分:-1)

为什么不进行两次查询?

SELECT * FROM table_name
WHERE Type = 15
GROUP BY Type

UNION ALL

SELECT * FROM table_name
WHERE Type <> 15