我是SQL新手,所以這個問題可能很愚蠢。找到最受歡迎的未成年人CS
下面是相關表的一個樣本行: 學生:
+-------+----------+---------+------+------+-------+---------+-----------+
| StuID | LName | Fname | Age | Sex | Major | Advisor | city_code |
+-------+----------+---------+------+------+-------+---------+-----------+
| 1001 | Smith | Linda | 18 | F | 600 | 1121 | BAL |
Minor_in:
+-------+------+
| StuID | DNO |
+-------+------+
| 1004 | 520 |
教研室:
+------+----------+--------------------------+------+--------------+--------+
| DNO | Division | DName | Room | Building | DPhone |
+------+----------+--------------------------+------+--------------+--------+
| 10 | AS | History of Art | 268 | Mergenthaler | 7117 |
我想找到最流行未成年人在CS專業(DNO = 600) 這裏是我的查詢,我噸的作品,但它的醜陋:
SELECT DNO, count
FROM (SELECT DNO, COUNT(*) AS count
FROM Minor_in
WHERE StuID IN (SELECT StuID
FROM Student
WHERE (Major = 550 OR Major = 600))
GROUP BY DNO
) AS C1
WHERE count IN (
SELECT MAX(count)
FROM (
SELECT DNO, COUNT(*) AS count
FROM Minor_in
WHERE StuID IN (SELECT StuID
FROM Student
WHERE (Major = 550 OR Major = 600))
GROUP BY DNO
) AS MinorCount
);
有沒有更好的辦法? 非常感謝!
謝謝!第一次使用StackOverflow!是要改變格式,但你只是太快了!如果我將stuid作爲Student,再次感謝 – BersaKAIN