mysql - SQL 按组计数

标签 mysql select join nested

我有一个看起来像这样的表

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/

相关文章:

php - 发生数据库错误

php - MySQL 命令行客户端问题 + 错误 2003

mysql - mysql根据另外两个表中的值将数据插入表中

加入多列的 SQL 查询

mysql - 识别 2 个表中的不同行

Android , Dialog Fragment 与 Mysql 数据库使用 Volley

mysql - 解决mysql中的 "Illegal mix of collations"错误

sqlite - 使用 Persist 将 Sqlite 表中所有记录中的一列提取到 Haskell 中的串联字符串中

mysql - 选择计数索引

sql - 在sql server中查找特定数据的最大值