• SQL三表连接查询与集合的并、交、差运算查询


    1. use db_sqlserver2  
    2. select 姓名, 工资, 面积, 金额, (工资+金额/1000) as 实发工资 from 职工,仓库, 订购单   
    3.    where 职工.职工号=订购单.职工号 and 职工.仓库号=仓库.仓库号  


    2:

    [sql] view plain copy
     
     
    1. select 姓名,工资,金额 from 职工,订购单 where 姓名 like '%平%' and 职工.职工号 = 订购单.职工号 order by 工资 desc  
    2.      

    3:

    [sql] view plain copy
     
     
    1. select 姓名,工资,金额 from 职工,订购单 where 姓名 like '%平%' and 职工.职工号 = 订购单.职工号 order by 工资 desc, 金额 desc  
    2.   
    3.      

    4:

    [sql] view plain copy
     
     
    1. select 姓名, 工资, 城市, (select AVG(工资) from 职工) as 平均工资 , (工资-(select AVG(工资) from 职工)) as 与平均工资之差  
    2. from 职工, 仓库 where 仓库.仓库号=职工.仓库号  

    5:带保存功能的多表连接查询

        在SQL语句中,利用into语句可以把查询到的结果保存成一张新表,然后再对新表进行数据信息查询。

        

    [sql] view plain copy
     
     
    1. select 仓库.仓库号, 城市, 面积, 姓名, 工资, 金额 into 多表连接产生的新表 from 仓库, 职工, 订购单   
    2. where 仓库.仓库号=职工.仓库号 and 职工.职工号=订购单.职工号  
    [sql] view plain copy
     
     
    1. select * from 多表连接产生的新表  


    //查看不同仓库中的所有职工的仓库号、平均销售金额、最大销售金额、最小销售金额、最大销售金额与最小销售金额之差的信息

    [sql] view plain copy
     
     
    1. select 仓库号, AVG(金额) as 平均销售金额, MAX(金额) as 最大销售金额, MIN(金额) as 最小销售金额,   
    2. (MAX(金额) - MIN(金额)) as 最大金额与最小金额之差 from 多表连接产生的新表 group by 仓库号;  


    可以把分组查询结果再生成一张新表

    [sql] view plain copy
     
     
    1. select 仓库号, AVG(金额) as 平均销售金额, MAX(金额) as 最大销售金额, MIN(金额) as 最小销售金额,   
    2. (MAX(金额) - MIN(金额)) as 最大金额与最小金额之差 into 分组查询产生的新表  
    3.  from 多表连接产生的新表 group by 仓库号;  
    4.    
    5.  select * from 分组查询产生的新表  

    6: 内连接查询(inner join)

        使用比较运算符对表之间的某些数据进行比较,并列出这些表中与连接条件相匹配的数据行。

        

    [sql] view plain copy
     
     
    1. select 姓名, 城市 from 仓库 inner join 职工 on 职工.仓库号=仓库.仓库号  

       多表的内连接查询

       

    [sql] view plain copy
     
     
    1. select 城市,面积, 姓名, 工资, 金额 from 仓库     
    2.   inner join 职工 on 职工.仓库号=仓库.仓库号  
    3.   inner join 订购单 on 职工.职工号=订购单.职工号  
    4.   and 工资>1800 and 面积<1000 and 金额 != 16600  

       

    7:左连接查询(left join)

          除满足连接条件的记录显示外,第一张表中不满足条件的记录也显示在结果集中。

        

    [sql] view plain copy
     
     
    1. select 姓名, 城市 from 仓库   
    2.    left join 职工 on 职工.仓库号=仓库.仓库号 and 城市 is not null and 姓名 like '%王%'  

    [sql] view plain copy
     
     
    1. select 城市, 面积, 姓名, 工资, 金额 from 仓库   
    2.    left join 职工 on 职工.仓库号 = 仓库.仓库号  
    3.    left join 订购单 on 职工.职工号=订购单.职工号  
    4.    and 工资>1800 and 面积<1000 and 金额!=16600  
    5.      


      在第一个left join左连接中,第一张表是仓库表,第二张表是职工表,在第二个left join左连接中,第一张表是职工表,第二张表是订购单表

    8:右连接查询

          除满足连接条件的记录显示外,第二张表中不满足条件的记录也显示在查询结果集中

         

    [sql] view plain copy
     
     
    1. select 姓名, 城市 from 仓库   
    2. right join 职工 on 职工.仓库号=仓库.仓库号 where 城市 is not null and 姓名 like '%王%'  

      

    [sql] view plain copy
     
     
    1. select 城市, 面积, 姓名, 工资, 金额 from 仓库   
    2.    right join 职工 on 职工.仓库号=仓库.仓库号  
    3.    right join 订购单 on 职工.职工号=订购单.职工号  
    4.    and 工资>1500 and 面积<1000 and 金额!=16600  


    [sql] view plain copy
     
     
    1. select 城市, 面积, 姓名, 工资, 金额 from 仓库   
    2.    right join 职工 on 职工.仓库号=仓库.仓库号  
    3.    right join 订购单 on 职工.职工号=订购单.职工号  
    4.    <span style="color:#ff0000;">where</span> 工资>1500 and 面积<1000 and 金额!=16600  

    把and关键字换为where关键字后的效果图,会发现那些无用的数据没有了


    9:全连接查询

        除满足连接条件的记录显示外,两张表中的不能满足条件的记录也显示在查询结果集中

        

    [sql] view plain copy
     
     
    1. select 姓名,城市 from 仓库 full join 职工 on 职工.仓库号=仓库.仓库号 and 城市 is not null and  
    2. 姓名 like '%王%';  

    集合的交、并、差运算查询

    为了进行并、交、差运算,要求运算的两个查询结果具有相同的字段个数,并且对应的字段的值要出自同一个值域,即具有相同的数据类型和取值范围

     10:并运算(union)

             主要将两个或者更多个查询的结果组合为单个结果集,该结果集包含联合查询中的全部查询的全部行

            

    [sql] view plain copy
     
     
    1. select 仓库号 from 仓库 where 城市='北京'  
    2. union  
    3. select 仓库号 from 职工 where 工资>2000  
    [sql] view plain copy
     
     
    1. select 仓库号 from 仓库 where 城市='北京'  
    2. union  
    3. select 仓库号 from 职工 where 工资>2000  
    4.   
    5. select distinct 仓库.仓库号 from 仓库, 职工 where 仓库.仓库号=职工.仓库号 and (城市='北京' or 工资>2000)  
    6.    

    使用union all 保留重复行

    [sql] view plain copy
     
     
    1. select 仓库号 from 仓库 where 城市='北京'  
    2. union all  
    3. select 仓库号 from 职工 where 工资>2000  


    11:交运算(intersect)

      可以将两个select语句的查询结果通过交运算合并成一个查询结果

       

    [sql] view plain copy
     
     
    1. select 仓库号 from 仓库 where 城市='北京'  
    2. intersect  
    3. select 仓库号 from 职工 where 工资>2000  
    [sql] view plain copy
     
     
    1. select distinct 仓库.仓库号 from 仓库, 职工 where 城市='北京' and 仓库.仓库号=职工.仓库号 and 工资>2000  

    12:差运算(except)

         可以计算两个select查询结果之间的数据差,即返回在一个查询结果中存在,但在另一个查询结果中不存在的所有行。

        

    [sql] view plain copy
     
     
    1. select 仓库号 from 仓库 where 城市='北京'   
    2. except   
    3. select 仓库号 from 职工 where 工资>2900  
    [sql] view plain copy
     
     
    1. select 仓库号 from 仓库 where 城市='北京' and 仓库号 not in(select 仓库号 from 职工 where 工资>2900)  


  • 相关阅读:
    阶段3 1.Mybatis_03.自定义Mybatis框架_6.自定义Mybatis的编码-实现基于XML的查询所有操作
    阶段3 1.Mybatis_03.自定义Mybatis框架_5.自定义Mybatis的编码-创建两个默认实现类并分析类之间的关系
    阶段3 1.Mybatis_03.自定义Mybatis框架_4.自定义mybatis的编码-解析XML的工具类介绍
    阶段3 1.Mybatis_03.自定义Mybatis框架_3.自定义mybatis的编码-根据测试类中缺少的创建接口和类
    阶段3 1.Mybatis_03.自定义Mybatis框架_2.自定义Mybatis的分析-创建代理对象的分析
    阶段3 1.Mybatis_02.Mybatis入门案例_3.mybatis注解开发和编写dao实现类的方式
    阶段3 1.Mybatis_02.Mybatis入门案例_2.mybatis入门案例中的设计模式分析
    阶段3 1.Mybatis_02.Mybatis入门案例_1.mybatis的入门
    阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_07.环境搭建的注意事项
    阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_06.mybatis的环境搭建
  • 原文地址:https://www.cnblogs.com/shaoguan/p/6524050.html
Copyright © 2020-2023  润新知