我有一个看起来像这样的表
id name answer question
1 john correct 1
1 john correct 2
1 john correct 3
1 john wrong 4
2 lee wrong 1
2 lee correct 2
2 lee correct 3
3 ana correct 1
3 ana wrong 2
我希望能够获得所有唯一用户的列表,并查看他们答对了多少问题,错了多少问题。
我试过这样的东西,但我似乎无法让它工作:
SELECT id, user_id, name, question_id, (select count(answer) from table where answer = 'CORRECT') as correct, (select count(answer) from table where answer= 'WRONG') as wrong FROM table GROUP BY user_id
有人可以帮助我走上正轨吗?谢谢
最佳答案
试试这个:
SELECT name, answer, COUNT(*) FROM yourtablenamehere GROUP BY name, answer
关于mysql - SQL 按组计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13694463/