我有一个用于查询的表,但似乎找不到预期的结果。也许我的疑问是错的。
表尝试次数

|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

最佳答案

只需从results中删除GROUP BY

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

SQL Fiddle DEMO

关于mysql - 按名称搜索查询计数1个字段组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21844452/

10-10 07:42