错误是"组功能嵌套太深"?

时间:2017-05-19 04:20:29

标签: mysql min

尝试显示学生人数最少的部门名称。 像

SELECT department_name
FROM department natural join student
GROUP BY department_name
HAVING MIN(COUNT(*));

表格结构是=>

the question was

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

SELECT TOP 1 department_name, count(*) AS number_of_students
FROM department natural join student
GROUP BY department_name
ORDER BY number_of_students

它将为您提供按学生人数排序的部门列表。通过TOP 1仅选择第一行,您将只获得学生人数最少的部门。

答案 1 :(得分:0)

这样吗?

SELECT 
T1.department_name, COUNT(T2.department_id) totalCount
FROM department T1
LEFT JOIN student T2
ON T1.department_id = T2.department_id
GROUP BY T2.department_id
HAVING COUNT(T2.department_id) =
(
  SELECT  
  COUNT(T2.department_id) totalCount
  FROM department T1
  LEFT JOIN student T2
  ON T1.department_id = T2.department_id
  GROUP BY T2.department_id
  ORDER BY totalCount ASC
  LIMIT 1
)

SQLFIDDLE DEMO