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;

原文地址:https://www.cnblogs.com/ejllen/p/3630725.html