搜索查询按名称计算1个字段组

时间:2014-02-18 04:33:55

标签: mysql

我有一张表,我一直在使用我的查询,但我似乎无法找到预期的结果。也许我的查询错了。

表尝试

|ID | name | results   
| 1 | Paul | Passed        
| 2 | Paul | Passed   
| 3 | Paul | Failed            
| 4 | John | Failed   
| 5 | John | Passed

结果应该是

name | passed |  Failed  | 
Paul |   2    |    1     |   
John |   1    |    1     |

但我的结果是4行。两次显示他们的名字。

这是我的查询

SELECT name,
SUM( results =  "Passed" ) AS passed,
SUM( results = "Failed" ) AS failed
FROM attempts
GROUP BY results, name

2 个答案:

答案 0 :(得分:3)

只需从results

中删除GROUP BY即可
SELECT name,
SUM( results =  "Passed" ) AS passed,
SUM( results = "Failed" ) AS failed
FROM attempts
GROUP BY name

SQL Fiddle DEMO

答案 1 :(得分:2)

尝试此查询

SELECT name, SUM( results =  "Passed" ) AS passed,
SUM( results = "Failed" ) AS failed
FROM attempt
GROUP BY name

Sql fiddle

enter image description here