我有一张表,我一直在使用我的查询,但我似乎无法找到预期的结果。也许我的查询错了。
表尝试
|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
答案 0 :(得分:3)
只需从results
GROUP BY
即可
SELECT name,
SUM( results = "Passed" ) AS passed,
SUM( results = "Failed" ) AS failed
FROM attempts
GROUP BY name
答案 1 :(得分:2)
尝试此查询
SELECT name, SUM( results = "Passed" ) AS passed,
SUM( results = "Failed" ) AS failed
FROM attempt
GROUP BY name