Not exists巢狀呼叫例子

2022-03-19 19:34:37 字數 965 閱讀 3611

查詢選修了所有課程的學生的學號,姓名,年齡

select sno,sname,datediff(yyyy,birthdate,getdate()) 年齡

from student

where not exists(select *      

from course

where not exists(select *

from sc

where sno=student.sno

and sc.cno=course.cno));

這個查詢,查出的是選擇全部課程的學生的姓名。

對於這個語法,要分步理解,從最下邊的where看起,從下往上去看。

最下邊的乙個select(not exists後邊)是把學生編號和課程編號帶入,結果是查詢學生選課的記錄。加上not exists,就是學生沒選課,那麼加上前邊的從課程表的select,就是查詢出學生沒選的課程。最後在上面又加了乙個not exists,那麼就是不存在沒選課程的資料,也就是說,這個學生選擇了所有的課程,才會符合記錄。

這麼說能明白麼?看我下邊分的段

----------------以下查詢選擇全部課程學生的姓名

select sname

from student

where not exists --------此處不存在配合以上student,可知查詢的是不存在沒選課的學生

----------------以下查詢學生沒選課的資訊

(select *

from course

where

not exists -------此處增加不存在,配合上面從course的選擇可知查詢的是學生沒有選的課

----------------以下查詢查詢學生選課的資訊

(select *

from sc

where sno = student.sno and cno = course.cno));

資料庫中的巢狀Not Exists語句

最近在準備複試,看到一道sql查詢題,涉及到兩層not exists,不是很理解,檢視了乙個dalao的解析之後,才明白了啥意思。查詢選修了所有課程的學生 的姓名 select sname from s where not exists select from c where not exists ...

Dll巢狀呼叫

問題描述 有乙個公共的標頭檔案裡面有這麼一段描述 define dll import declspec dllimport define dll export declspec dllexport ifdef win32 ifndef dll export api define dll export...

mysql游標巢狀迴圈例子

create procedure finance recivedetail customer id varchar 20 begin declare done int default 0 declare id a bigint declare pre recv remain a decimal 12...