sql找到最老的学生

时间:2011-12-20 15:38:13

标签: mysql

SELECT s.sid, s.sname, COUNT(s.age) AS age
FROM student s
INNER JOIN enrolled e ON s.sid = e.sid
INNER JOIN class c ON e.ccode = c.ccode
INNER JOIN tutor t ON c.tid = t.tid
WHERE s.programme = 'CIS'
AND t.tname = 'Kathy Bond'
GROUP BY s,sid, s.sname
ORDER BY COUNT(s.age) DESC
LIMIT 1;

我需要找到参加Kathy bond班级的最老的CIS学生。这是我到目前为止所说的,它说无法识别订购运营商?这些是表格:

类别:

ccode   | cname         | weekday | meets at | room  | tid
________________________________________________________________________
CIS166  | Intro to Comp | Tue     | 10:00:00 | MB300 | 1
________________________________________________________________________
CIS177  | Maths         | Mon     | 15:00:00 | MB100 | 6
________________________________________________________________________
CIS188  | Info Syst     | Thu     | 14:00:00 | MB230 | 5
________________________________________________________________________
CIS199  | Intro to Java | Fri     | 10:00:00 | MB300 | 4
________________________________________________________________________
CIS277  | Algorithmics  | Thu     | 14:00:00 | MB200 | 2
________________________________________________________________________
CIS288  | Database Syst | Mon     | 14:00:00 | MB142 | 1
________________________________________________________________________
CIS297  | Web Design    | Mon     | 16:00:00 | MB109 | 3
________________________________________________________________________
CIS298  | C++           | Fri     | 10:00:00 | MB110 | 8

导师:

tid | tname
_____________________
1   | Rob Hoffman
_____________________
2   | James Butler
_____________________
3   | Kathy Bond
_____________________
4   | Theodora Stewart
_____________________
5   | Mike Richie
_____________________
6   | John Kay
_____________________
7   | Mary Tregear
_____________________
8   | Mark Robinson

已登记:

sid | ccode
______________
211 | CIS288
203 | CIS298
214 | CIS297
105 | CIS177
215 | CIS297
104 | CIS188
210 | CIS297
338 | CIS320
102 | CIS177
338 | CIS399
204 | CIS288
204 | CIS277
102 | CIS199
203 | CIS297
105 | CIS199
331 | CIS320
202 | CIS299
205 | CIS299
210 | CIS298
331 | CIS399
321 | CIS399
210 | CIS288
210 | CIS277
204 | CIS297
321 | CIS320
328 | CIS388
327 | CIS388
211 | CIS297
333 | CIS399
215 | CIS288
104 | CIS199

学生:

sid | sname        | programme | level | age
____________________________________________
101 | Lorry Ross   | CS         | 1     | 18
102 | Lydia Ken    | CIS        | 1     | 18
103 | Bob Chung    | CS         | 1     | 18
104 | Sonia Morris | CIS        | 1     | 18
105 | Mark Poppy   | CS         | 1     | 19
106 | Megan Chi    | IT         | 1     | 20
218 | Diana McDon  | IT         | 2     | 20
219 | Nick Smith   | IT         | 2     | 21

3 个答案:

答案 0 :(得分:2)

我认为你不需要GROUP BY。只需ORDER BY和LIMIT 1即可。试试这个:

SELECT s.sid, s.sname, s.age
FROM student s
INNER JOIN enrolled e ON s.sid = e.sid
INNER JOIN class c ON e.ccode = c.ccode
INNER JOIN tutor t ON c.tid = t.tid
WHERE s.programme = 'CIS'
AND t.tname = 'Kathy Bond'
ORDER BY s.age DESC
LIMIT 1

答案 1 :(得分:1)

“最早的”意味着MAX(age)

答案 2 :(得分:0)

问题在于:     按计数排序(s.age)DESC

它应该是:     ORDER BY s.age DESC

SELECT语句的第一部分中有COUNT,它就是它所在的位置。