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

    xiaoxiao2025-08-12  6

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

    2:

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

    3:

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

    4:

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

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

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

        

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

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

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

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

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

    6: 内连接查询(inner join)

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

        

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

       多表的内连接查询

       

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

    7:左连接查询(left join)

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

        

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

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

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

    8:右连接查询

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

         

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

      

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

    [sql]  view plain  copy select 城市, 面积, 姓名, 工资, 金额 from 仓库       right join 职工 on 职工.仓库号=仓库.仓库号      right join 订购单 on 职工.职工号=订购单.职工号      <span style="color:#ff0000;">where</span> 工资>1500 and 面积<1000 and 金额!=16600   把and关键字换为where关键字后的效果图,会发现那些无用的数据没有了

    

    9:全连接查询

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

        

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

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

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

     10:并运算(union)

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

            

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

    使用union all 保留重复行 [sql]  view plain  copy select 仓库号 from 仓库 where 城市='北京'   union all   select 仓库号 from 职工 where 工资>2000  

    11:交运算(intersect)

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

       

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

    12:差运算(except)

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

        

    [sql]  view plain  copy select 仓库号 from 仓库 where 城市='北京'    except    select 仓库号 from 职工 where 工资>2900   [sql]  view plain  copy select 仓库号 from 仓库 where 城市='北京' and 仓库号 not in(select 仓库号 from 职工 where 工资>2900)  
    转载请注明原文地址: https://ju.6miu.com/read-1301683.html
    最新回复(0)