我正在尝试在MySQL中编写查询以执行以下操作:

“如果该部门没有没有分配顾问的学生,请找到建议最多的现有教师,然后将其中一名顾问重新分配给新教师。”

表定义为:

Advisor(i_id, s_id) - i_id, s_id is the primary key.
Instructor(i_id, name, dept_name, salary) - i-id is the primary key.
Student(s_id, name, dept_name, tot_cred) - s_id is the primary key.

我已经编写了以下查询,该查询是不完整的,但是我不确定如何进行:

SELECT MAX(counted) 
(SELECT i_id, COUNT(s_id) AS counted
 FROM Advisor
 GROUP BY i_id) ;

任何帮助将不胜感激!!

===============>>#1 票数:1 已采纳

没有分配顾问的部门中没有学生:

SELECT Student.s_id from Student
WHERE s_id NOT IN ((SELECT DISTINCT(s_id) from Advisor) AS Assigned)

找到建议最多的现有教师

SELECT MAX(value), i_id
FROM
(SELECT i_id, COUNT(*) as value
FROM Advisor
GROUP BY i_id) AS counted) 

将他们的建议之一重新分配给新教练

Done Manually

还没有测试过,这里可能有语法错误

  ask by pypep278 translate from so

未解决问题?本站智能推荐: