我有三张桌子:
我想创建一个mysql查询;给定user_id,将获取用户加入的所有课程。此外,对于每个课程行,我想添加一个列,表示该课程中有多少用户。
我做了一个查询,但它没有给我想要的结果:
SELECT courses.id AS CourseID, courses.name AS Name, COUNT(usersCourses.userId) AS JoinedUsersNumber
FROM courses, users, usersCourses
WHERE users.id = courses.userId
AND usersCourses.courseId = courses.id
AND usersCourses.courseId=courses.id
AND usersCourses.userId='31'
有人能告诉我如何实现这个目标或者告诉我我的查询有什么问题吗?
修改 这是一个例子:
用户表
id | username
--------------------------
u1 | user01
u2 | user02
u3 | user03
u4 | user04
课程表:
id | name
--------------------------
c1 | course01
c2 | course02
c3 | course03
c4 | course04
userscourses表:
userId | courseId
--------------------------
u1 | c1
u1 | c2
u1 | c3
u2 | c1
u3 | c1
u4 | c1
u2 | c2
u3 | c2
预期结果(user_id = u1):
name | Joined Users
--------------------------
course01 | 4
course02 | 3
course03 | 1
答案 0 :(得分:1)
加入userscourses表以获得该课程的所有用户: -
SELECT courses.id AS CourseID, courses.name AS Name, COUNT(usersCourses2.userId) AS JoinedUsersNumber
FROM users
INNER JOIN usersCourses
ON users.id = usersCourses.userId
INNER JOIN courses
ON usersCourses.courseId = courses.id
INNER JOIN usersCourses usersCourses2
ON usersCourses.courseId = usersCourses2.courseId
WHERE usersCourses.userId='31'
GROUP BY courses.id AS CourseID, courses.name AS Name
或者如果您希望该课程的用户数排除所选用户(即您想要其他用户数): -
SELECT courses.id AS CourseID, courses.name AS Name, COUNT(usersCourses2.userId) AS JoinedUsersNumber
FROM users
INNER JOIN usersCourses
ON users.id = usersCourses.userId
INNER JOIN courses
ON usersCourses.courseId = courses.id
LEFT OUTER JOIN usersCourses usersCourses2
ON usersCourses.courseId = usersCourses2.courseId
AND users.id != usersCourses2.userId
WHERE usersCourses.userId='31'
GROUP BY courses.id AS CourseID, courses.name AS Name
答案 1 :(得分:0)
首先进行一个返回课程用户计数的查询:
SELECT course.id, course.name, count(*) as JoinedUsersNumber
FROM course
JOIN usersCourses
ON course.id=usersCourses.courseId
GROUP BY course.id, course.name
然后将其用作子查询,如
SELECT
c.id AS CourseID,
c.name AS Name,
c.JoinedUsersNumber
FROM
SELECT course.id, course.name, count(*) as JoinedUsersNumber
FROM course
JOIN usersCourses
ON course.id=usersCourses.courseId
GROUP BY course.id, course.name
) c
JOIN
usersCourses ON c.id = usersCourses.courseId =
JOIN
users ON users.id = usersCourses.userId
WHERE usersCourses.userId=31
答案 2 :(得分:0)
您的查询是
SELECT courses.id AS CourseID, courses.name AS Name, COUNT(usersCourses.userId) AS JoinedUsersNumber
FROM courses, users, usersCourses
WHERE users.id = courses.userId
AND usersCourses.courseId = courses.id
AND usersCourses.courseId=courses.id
AND usersCourses.userId='31'
根据您的架构
users.id = courses.userId
,但您的课程表中没有userId AND usersCourses.courseId = courses.id AND usersCourses.courseId = courses.id
希望这会对你有所帮助。
SELECT courses.id AS CourseID, courses.name AS Name, COUNT(usersCourses.userId) AS JoinedUsersNumber
FROM courses, users, usersCourses
WHERE
usersCourses.courseId = courses.id
AND usersCourses.userId=users.userId
AND usersCourses.userId='31'
Group By courses.name
答案 3 :(得分:0)
这是我的SQL小提琴:http://sqlfiddle.com/#!9/6a134/4/0
SELECT *
FROM
(
SELECT
userscourses.user_id AS user_id,
users.user_name AS user_name,
userscourses.course_id AS course_id,
courses.course_name AS course_name
FROM
users, courses, userscourses
WHERE
users.user_id = userscourses.user_id
AND courses.course_id = userscourses.user_id
AND userscourses.user_id = 1
) userTable
LEFT JOIN
(
SELECT
course_id,
COUNT(user_id) AS sum_participant
FROM
userscourses
GROUP BY
course_id
) courseTable
ON userTable.course_id = courseTable.course_id
答案 4 :(得分:0)
这样可行:
Can't open perl script "script.pl" : No such file or directory