2011-12-20 67 views
0
SELECT DISTINCT s.sname, s.sid 
FROM student s, class c 
WHERE s.programme = 'CS' 
AND s.level = '2' 
AND c.week_day = 'Mon' 
AND meets_at < '12:00:00' 
AND meets_at > '12:00:00'; 

在這個查詢中,我一直在努力的位是它的時間部分。我需要選擇在上午和下午星期一上課的2級CS學生。它不顯示任何結果sql時間查詢多於/小於

Class: 

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 
Tutor: 

    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 
Enrolled: 

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 
Student: 

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 
+0

表結構? 「學生」和「課堂」如何鏈接? – 2011-12-20 16:28:22

+0

這就是他們沒有鏈接的東西。 – 2011-12-20 16:36:28

+0

你有鏈接表'StudentClasses'嗎?如果不是,你怎麼知道學生正在上哪些課? – 2011-12-20 16:37:45

回答

1

最後2個標準將百達是假在一起 - 你想,其實加入2班有一個學生,所以你需要

SELECT DISTINCT s.sname, s.sid 
FROM student s 
INNER JOIN class cmorning ON <join criterium here> 
INNER JOIN class cafternoon ON <join criterium here> 
WHERE s.programme = 'CS' 
AND s.level = '2' 
AND cmorning.week_day = 'Mon' 
AND cafternoon.week_day = 'Mon' 
AND cmorning.meets_at < '12:00:00' 
AND cafternoon.meets_at > '12:00:00' 
GROUP BY s.sid 
; 
0

這將返回列表學生將在星期一參加一個以上的CS課程

SELECT DISTINCT s.sname, s.sid 
FROM student s 
INNER JOIN Enrolled e on s.sid = e.sid 
INNER JOIN class day on day.ccode = e.ccode 
WHERE s.programme = 'CS' 
AND s.level = '2' 
AND c.week_day = 'Mon' 
GROUP BY s.sid 
HAVING COUNT(s.sid) > 1