Sql高级查询(三)

一.联合查询:将多次查询(多条select语句),在记录上拼接(字段不会增加)

          1. 基本语法:多条select构成,每条select语句字段数保持一致(字段类型可以不一致)

                                1):select   语句1 union[选项] select 语句2 union[选项]

                                2):union[选项]:all(不管重复)保留所有、distinct去重(必须一条记录全部重复)默认

                                       select * from my_class union distinct select name,sex,course from student;

                                3):联合查询的意义

                                               1.查询同一张表,但需求不同,男生降序,女生升序

                                                   在联合查询中使用order by查询两个查询语句都必须用(),搭配limit使用       

                                                (select * from student where sex=’male’ order by age asc limit 100) union (select *                                                            from student where sex=’female’ order by age desc limit 100);

                                                2.查询多张表多张表的结构完全一样,保存的数据(结构)也一样

二.子查询:一个查询结果作为另一个查询的条件

        1.子查询分类:按位置分类、结果分类

                    1):按位置查询:子查询(select语句)在外部查询(select语句)中出现的位置。

                                     1.from子查询:子查询在from之后

                                     2.where子查询:子查询在where之后

                                     3.exists子查询:子查询在exists里面

                    2):按照结构子查询:根据子查询得到的结果进行分类

                                     1.标量子查询:子查询得到的结果是一行一列,例如一致课程名称查询id

                                           select * from student where id=(select id from student where course=’Java’);
                                           +—-+——–+——+——+——–+
                                            | id | name   | sex  | age  | course |
                                           +—-+——–+——+——+——–+
                                            |  1 | steven | male |   20 | Java   |
                                           +—-+——–+——+——+——–+

                                     2.列子查询:子查询得到的结果是一列多行,需要使用in作为查询条件,类似的还有all、some、any

                                            select * from student where id =some(select id from student);

                                             使用all、some、any时必须在前面加=号

                                            select * from student where id in(select id from student);

                                            +—-+——–+——–+——+——–+
                                             | id | name   | sex    | age  | course |
                                            +—-+——–+——–+——+——–+
                                             |  1 | steven | male   |   20 | Java   |
                                             |  2 | mary   | female |   27 | PHP    |
                                             |  3 | candy  | female |   24 | C语言  |
                                             |  4 | mark   | male   |   22 | C++    |
                                             |  5 | hary   | female |   26 | JavaEE |
                                            +—-+——–+——–+——+——–+

                                     3.行子查询:子查询得到的结果时多行一列(多行多列),需要构造行元素,它由多个字段构成

                                    select * from student where id=(select max(id) from student) or age=(select max(age) from

                                    student);

                                     select * from student where (id,age)=(select max(id),max(age) from student);

                                     4.表子查询:子查询得到的结果是多行多列(位置在from之后),返回结果当作二维表来使用

                                     select * from (select * from student order by height desc) as stu group by sex;

                                     5.exists子查询:用来判断某些查询条件是否满足(跨表查询),exists实在where之后返回结果0和1

                                        select * from student where exists (select * from my_class);

    原文作者:SQL
    原文地址: https://blog.csdn.net/SuperstarSteven/article/details/79835189
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞