2014-09-25 102 views
-1

我與sqlite3的工作,我有4個表:SQLite的3查詢:尋找人與最大值

FROM classmate s, tookclass t, class c, classsection sec 
WHERE s.sid = t.sid AND sec.secid = t.secid AND c.dept = 'CompSci'; 

我的代碼在這裏將輸出在「CompSci」的部門已經採取了班上所有學生ID 。

我很困惑。

回答

1
Select * from Classmate s  
where Exists (Select * From Class c 
       join ClassSection cs on cs.crsid = c.crsid 
       join tookClass tc on tc.secid = cs.secid 
       where tc.sid = s.sid 
        and c.dept = 'CompSci' 
       group by tc.sid 
       having count(*) = 1) 

Select * from Classmate s  
where Exists (Select * From Class c 
       join ClassSection cs on cs.crsid = c.crsid 
       join tookClass tc on tc.secid = cs.secid 
       where tc.sid = s.sid 
        and c.dept = 'CompSci') -- there is one, .... 
    and Not exists 
      (select * from Class c1 
       join ClassSection cs1 on cs1.crsid = c1.crsid 
       join tookClass tc1 on tc1.secid = cs1.secid 
       where tc.sid = s.sid 
        and c1.dept = 'CompSci' 
        and c1.crsid <> c.crsid) -- but there is NOT another 
+0

謝謝,非常有幫助! – 2014-09-25 17:08:44

+0

對不起,錯字,再試一次... – 2014-09-25 17:12:11

+0

這是非常非常有幫助。我認爲在完成輸入之前我已經嘗試了代碼。再次感謝。 – 2014-09-25 17:24:49