条件GROUP BY结果

时间:2011-07-25 08:37:58

标签: mysql group-by conditional

我需要查询按name进行分组,如果is_new一个1,则结果值应为1


当前行:

+--------+--------+
| name   | is_new |
+--------+--------+
| a      |      0 |
| a      |      0 |
+--------+--------+
| b      |      0 |
| b      |      1 |
+--------+--------+
| c      |      1 |
| c      |      1 |
+--------+--------+

预期查询结果:

+--------+--------+
| name   | is_new |
+--------+--------+
| a      |      0 |
| b      |      1 |
| c      |      1 |
+--------+--------+

1 个答案:

答案 0 :(得分:6)

SELECT name,MAX(is_new) AS is_new FROM <TABLE> GROUP BY name