一切!我正在上数据库课程并且有几个问题让我很困惑。下面是我的桌子
Student(S_ID, S_FIRST_NAME, S_LAST_NAME, S_MAJOR)
Course(C_ID, C_NAME, C_INST_NAME, C_ROOM)
takes(S_ID,C_ID)
Q-1.i想要选择所有参加Davidson教授的所有课程的student_id。我尝试使用此代码
select s.S_ID from student s inner join (select t.S_ID from takes t inner join
course c on t.C_ID = c.C_ID group by t.S_ID having sum(case when c.C_INST_NAME
= 'Davidson' then 1 else 0 end) = 3) t on s.S_ID = t.S_ID;
它有效,因为我知道戴维森教了多少课(在我的例子中为3)。如果我们不知道他教了多少课程,我们如何编写查询?
Q-2。我想选择所有教授至少3个班级的教师。对于这个问题我做了以下
select distinct C_INST_NAME from course where C_ID >= 3;
+-------------+
| C_INST_NAME |
+-------------+
| Peterson |
| Davidson |
| Jackson |
| Hanney |
+-------------+
但我得到了所有的导师,任何帮助都将不胜感激谢谢!
答案 0 :(得分:-1)
Q1。
SELECT
S_ID, GROUP_CONCAT(C_ID ORDER BY C_ID ASC)
FROM
takes
WHERE
GROUP_CONCAT(C_ID ORDER BY C_ID ASC) = (
SELECT
GROUP_CONCAT(Course.C_ID ORDER BY C_ID ASC)
FROM
Course
WHERE
Course.C_INST_NAME = "Davidson"
)
GROUP BY
S_ID
Q2。
SELECT
COUNT(C_ID) AS "numClassesTaught", Course.C_INST_NAME
FROM
Course
GROUP BY
Course.C_INST_NAME
HAVING
numClassesTaught >= 3
那些应该都有效。