Mysql查询只提供一条记录,但count函数给出的计数不止一个。
[编辑] 我的基本目标是计算尝试调查的用户。在表格中,每一行代表调查中每个问题的答案。所以每行都包含用户ID。
数据库表架构(表格):
+-------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| survey_id | int(10) unsigned | NO | | NULL | |
| user_id | int(10) unsigned | NO | | NULL | |
+-------------+------------------+------+-----+---------------------+----------------+
表格(表格)中的数据:
+----+-----------+---------+
| id | survey_id | user_id |
+----+-----------+---------+
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 1 |
+----+-----------+---------+
3 rows in set (0.00 sec)
COUNT个查询:
SELECT count(*) AS total FROM `table` WHERE survey_id = 1 GROUP BY user_id
结果:
+-------+
| total |
+-------+
| 3 |
+-------+
1 row in set (0.00 sec)
简单查询:
SELECT id,user_id,survey_id AS total FROM `table` WHERE survey_id = 1 GROUP BY user_id
结果:
+----+---------+-------+
| id | user_id | total |
+----+---------+-------+
| 1 | 1 | 1 |
+----+---------+-------+
1 row in set (0.00 sec)
欲望结果:
行数和行数应相同。
所以,为什么第二个查询返回单行和第一个计数查询返回的不是一个。
让我知道是否仍然不清楚。
任何帮助都会提前发现。
答案 0 :(得分:1)
如果您想计算群组GROUP BY
返回的数量,可以使用COUNT(distinct user_id)