Mysql SQL计数返回错误的数字
因此,我的查询有问题。我有两张桌子: 课程: 此表中的Mysql SQL计数返回错误的数字,mysql,sql-server,Mysql,Sql Server,因此,我的查询有问题。我有两张桌子: 课程: 此表中的用户id是本课程的讲师 ----------------------------------------------------------------------- | course_id | user_id | course_name | other information | -------------------------------------------------------------------
用户id
是本课程的讲师
-----------------------------------------------------------------------
| course_id | user_id | course_name | other information |
-----------------------------------------------------------------------
| 6 | 1 | My Course 1 | ... |
-----------------------------------------------------------------------
--------------------------------------------------
| user_id | course_id | created_at |
--------------------------------------------------
| 5 | 6 | [UNIX_TIMESTAMP] |
--------------------------------------------------
我的大学课程:
此表中的用户id
是本课程的学生
-----------------------------------------------------------------------
| course_id | user_id | course_name | other information |
-----------------------------------------------------------------------
| 6 | 1 | My Course 1 | ... |
-----------------------------------------------------------------------
--------------------------------------------------
| user_id | course_id | created_at |
--------------------------------------------------
| 5 | 6 | [UNIX_TIMESTAMP] |
--------------------------------------------------
myu课程
包含参加该课程的人数。我想获得所有课程信息以及参加课程的人数。除了参加课程的人数外,一切都按预期进行。这是我正在使用的查询:
SELECT
courses.*,
users.name, //This is the name of the instructor
users.last_name, //This is the last name of the instructor
COUNT(my_courses.user_id) as count_students
FROM courses
LEFT JOIN users
ON courses.user_id = courses.user_id
LEFT JOIN my_courses
ON courses.course_id = my_courses.course_id
WHERE courses.course_id = '6'
就像我说的,这个查询像普通一样返回课程信息,但是当它应该只返回
1
时,它返回3
作为count\u students
。有人知道为什么会这样吗?非常感谢您的帮助 这可能是因为用户的加入
条件。应该是:
LEFT JOIN users
ON courses.user_id = users.user_id
您还应该在查询中添加GROUP BY
子句:
SELECT
c.*,
u.name,
u.last_name,
COUNT(mc.user_id) AS count_students
FROM courses c
LEFT JOIN users u
ON c.user_id = u.user_id
LEFT JOIN my_courses mc
ON c.course_id = mc.course_id
WHERE c.course_id = '6'
GROUP BY
<columns not in the aggregate function>
选择
c、 *,
u、 名字,
u、 姓,
计数(mc.user\U id)为计数学生
来自课程c
左加入用户u
在c.user\u id=u.user\u id上
左加入我的课程
在c.course\u id=mc.course\u id上
其中c.course_id='6'
分组
此外,为表添加别名以提高可读性。联接操作会导致行的组合乘法。您需要从自己的表中汇总学生人数,如下所示
SELECT course_id, COUNT(*) students
FROM my_courses
GROUP BY course_id
这将为您提供一个每个课程id包含一行或零行的结果集。然后,您可以将其连接到查询的其余部分
SELECT courses.*,
users.name, //This is the name of the instructor
users.last_name, //This is the last name of the instructor
aggr.count_students
FROM courses
LEFT JOIN users ON courses.user_id = courses.user_id
LEFT JOIN (
SELECT course_id, COUNT(*) students
FROM my_courses
GROUP BY course_id
) aggr ON courses.course_id = aggr.course_id
WHERE courses.course_id = '6'
这样,您就可以避免多次计算您的学生参加可能有多个讲师的课程。请标记正确的RDBMS。您需要一个GROUP BY
子句来获得正确的计数。可能是对返回的行进行计数,而不是从my_courses
表中进行计数。您的示例数据对问题没有帮助。ON courses.user_id=courses.user_id
。。。这是打字错误吗?是的,我现在才注意到。哈哈,它现在起作用了是的,这就是我刚才注意到的。让我试试,原来是这个打字错误。谢谢你的建议,我会记住的。再次感谢!:)