我的sql语句如下:
SELECT course_id FROM (
(SELECT COUNT(course_id) AS count FROM takes GROUP BY course_id) AS result)
WHERE count IN (SELECT MAX(count) FROM result)
如何使用临时表两次?例如,表格“结果”在这里。
答案 0 :(得分:0)
如果您只想获取具有最大拍摄数量的course_id
,则可以按计数对子查询进行排序,并将结果集限制为一条记录。
SELECT course_id, COUNT(course_id) AS count
FROM takes
GROUP BY course_id
ORDER BY COUNT(course_id) DESC
LIMIT 1
如果您还想获得所有课程ID值,如果最大拍摄数量达到平局,那么您可以尝试以下稍微复杂的查询:
SELECT result.course_id
FROM
(
SELECT course_id, COUNT(course_id) AS count
FROM takes
GROUP BY course_id
) AS result
WHERE result.count = (
SELECT COUNT(*)
FROM takes
GROUP BY course_id
ORDER BY COUNT(*) DESC
LIMIT 1
)