MySQL 查詢同一字段中同時知足多個條件

 

分析:學習

1,先查詢出01號同窗學習的課程spa

SELECT C FROM sc WHERE S='01'

 2,查詢學習該相關課程的同窗編號code

SELECT S FROM sc WHERE C in (SELECT C FROM sc WHERE S='01')

 能夠看到,咱們分別查詢了3次,因此出現多個結果,由於是or關係,因此每一個選擇了一、二、3課程的同窗都所有取出,blog

3,如今須要將出現次數爲3的編號取出class

SELECT S FROM sc WHERE C in (SELECT C FROM sc WHERE S='01') GROUP BY S HAVING COUNT(S) =3

 看到編號爲一、二、三、4的同窗選了與01號同窗一致的課程im

4,與student表進行連表查詢,取出相關信息查詢

SELECT a.* FROM student a LEFT JOIN sc b ON a.S = b.S WHERE b.S in ( SELECT S FROM sc WHERE C in (SELECT C FROM sc WHERE S='01') GROUP BY S HAVING COUNT(S) =3) GROUP BY a.S 

相關文章
相關標籤/搜索