sql给我的课程里面的用户不到30人

xlpyo6sf  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(368)

我正在寻找一个解决方案,以获得一个课程列表,其中有少于 30 里面的用户。我到处都找过了,但都找不过去。我对mysql不是很实用。
我有三张table:

1) TABLE course
_ID, COURSECODE

2) TABLE course_user:
_ID, ID_COURSE, ID_USER

3) TABLE user:
_ID

我怎样才能拿到票 COURSECODE 只有少于 30 用户?
非常感谢你。。。对不起,我英语不好。

5vf7fwbs

5vf7fwbs1#

这里有一个想法:

SELECT COURSECODE, COUNT(user:_ID) FROM TABLE1, TABLE3
WHERE 
   TABLE3.user:_ID = TABLE2.course_user:_ID
AND
   TABLE2.ID_COURSE = TABLE1.ID_USER
GROUP BY COURSECODE
HAVING COUNT(TABLE2.ID_USER) < 30

请避免使用无意义或混淆的名称。如 user: _ID , ID_USER , course_user: _ID 有关此查询的更多信息,我建议您查看:
计数文档
按文档分组
有文件

wj8zmpe1

wj8zmpe12#

试试这个:

SELECT c.COURSECODE, count(u.id) as userCount
FROM course_user cu JOIN course c on (c.id = cu.id_course) JOIN user u on (u.id = cu.id_user)
GROUP BY c.COURSECODE
HAVING userCount < 30;
ars1skjm

ars1skjm3#

看看having子句:http://www.mysqltutorial.org/mysql-having.aspx
像这样:

select Course.COURSECODE, count(course_user.ID_USER) from course
     join course_user on course_user .ID_COURSE = course._ID
     group by Course.COURSECODE
     having count(course_user.ID_USER) < 30
z31licg0

z31licg04#

您可以使用:

SELECT c.COURSECODE
  FROM course_user cu 
  LEFT JOIN course c on (c.id = cu.id_course)
  LEFT JOIN user u on (u.id = cu.id_user)
 GROUP BY c.COURSECODE
 HAVING count(cu.id_user) < 30

相关问题