sql子查詢

一、子查詢入門:

1、單值子查詢:

        單值子查詢的唯一限制:子查詢的返回值必須只有一行記錄,而且只能有一列(又被稱爲標量子查詢)。

        可以使用在select語句的列表、表達式中,以及where語句中等。

        例1:select 1 as f1,2,(select min(fyearpublished) from t_book),(select max(fyearpublished) from t_book) as f4 from dual;

2、列值子查詢:

        列值子查詢可返回一個多行多列的結果集(又稱爲表子查詢)。

        可以使用在select語句的from子句中、insert語句、連接、in子句等。

        例1:select t_reader.fname,t2.fyearpublished,t2.fname

        from t_reader,(select * from t_book where fyearpublished < 1800) t2;

        表子查詢可以看做一張臨時表,所以引用子查詢中列的時候必須使用子查詢中定義的列名,也就是如果子查詢中爲列定義了別名,那麼在引用的時候也要使用別名。

        例2:select t_reader.fname,t2.fyear,t2.fname,t2.f3

        from t_reader,(select fyearpublished as fyear,fname,1+2 as f3 from t_book where fyearpublished < 1800) t2;

二、select列表中的標量子查詢:

        例1:select fid,fname,(select max(fyearpublished) from t_book where t_book.fcategoryid = t_category.fid)

        from t_category;

        這裏的子查詢與前面的不同,這個子查詢必須依賴於外部查詢的字段,也就是可以直接單獨直行,而這裏的子查詢是依賴於外部查詢中的t_category.fid這個字段的,是無法單獨執行的。

三、where子句中的標量子查詢:

        例1:select freaderid from t_readerfavorite

        where fcategoryid=(

            select fid from t_category where fname='story'

        );

        例2:檢索每種類型中最早出版的圖書:

        select t_category.fid,min(t_book.fyearpublished)

        from t_category

        inner join t_book on t_category.fid=t_book.fcategoryid

        group by t_category.fid;

        在上一句的基礎上,加上書籍名稱:

        select t_category.fid,t_book.fyearpublished

        from t_category

        inner join t_book on t_category.fid=t_book.fcategoryid

        where t_book.fyearpublished=

        (

            select min(t_book.fyearpublished) from t_book where t_book.fcategoryid=t_category.fid

        );

        首先在兩個表內進行自連接,再在where子句中使用子查詢來過濾數據。

四、集合運算符與子查詢:

1、in運算符:

        例:檢索出所有圖書出版年份內入會的讀者信息:

        select * from t_reader

        where fyearofjoin in

        (select fyearpublished from t_book);

2、any和some運算符:

        在sql中any和some是同義詞,基本用法也相同。any必須和其他比較運算符共同使用,而且必須將比較運算符放在any關鍵字之前,所比較的值也需要匹配子查詢中的任意一個值:

        例:any和=運算符共同使用的例子,檢索出所有圖書出版年份內入會的讀者信息。“=any”等價於in

        select * from t_reader

        where fyearofjoin =any

        (select fyearpublished from t_book);

        any還可以和大於、小於、大於等於、小於等於等比較運算符共同使用。

        例:檢索出會員出生之前出版的圖書。

        select * from t_book

        where fyearpublished<any

        (select fyearofbirth from t_reader);

注意:any運算符不能和固定的集合相匹配,如:select * from t_book where fyearpublished<any(2001,2003,2005);

3、all運算符:

all運算符要求比較的值需要匹配子查詢中的所有值,不能單獨使用,同樣不能與固定值匹配。

        例:檢索出所有會員入會之前出版的圖書:

        select * from t_book

        where fyearpublished<all

        (select fyearofjoin from t_reader);

注意:如果匹配的集合爲空,也就是子查詢沒有返回任何數據的時候,不論與什麼比較運算符搭配使用all的返回值將永遠是true。

4、exists運算符:

    這個是單目運算符,不與列匹配,也不要求匹配的集合是單列。exists是用來檢查每一行是否匹配子查詢,可以認爲exists就是用來測試子查詢結果是否爲空的,如果結果集爲空則匹配結果爲false,否則匹配結果爲true。

         例:測試是否存在山東省的讀者:

         select * from t_book where exists

         (select * from t_reader where fprovince='ShanDong');

          例:檢索在1950年以前出版的圖書的圖書類別:

         select * from t_category where exists

         (select * from t_book

          where t_book.fcategoryid=t_category.fid and t_book.fyearpublished<1950);

五、子查詢在其他類型sql語句中的應用:

1、在insert語句中的應用:

例:insert into t_readerfavorite(fcategoryid,freaderid)

        //爲t_reader表中每一個讀者都在t_readerfavorite表中創建一條fcategoryid等於1的記錄

        select 1,fid from t_reader

        where not exists

        (select * from t_readerfavorite where t_readerfavorite.fcategoryid=1 and t_readerfavorite.freaderid=t_reader.fid);

2、在update語句中的應用:

例:將所有同類書本數超過3本的圖書的出版日期更新爲2005:

        update t_book b1

        set b1.fyearpublished=2005

        where(select count(*) from t_book b2 where b1.fcategoryid=b2.fcategoryid)>3;

3、在delete語句中的應用:

例:將所有同類書本數超過3本的圖書刪除:

      delete from t_book b1

      where(select count(*) from t_book b2 where b1.fcategoryid=b2.fcategoryid)>3;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章