SQLite 3查询:查找具有最大值的人员

时间:2014-09-25 16:56:51

标签: sql sqlite

我正在使用SQLite3,我有4个表:

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 个答案:

答案 0 :(得分: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