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

简介: use db_sqlserver2  select 姓名, 工资, 面积, 金额, (工资+金额/1000) as 实发工资 from 职工,仓库, 订购单      where 职工.职工号=订购单.
  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)  


 

目录
相关文章
|
8天前
|
SQL
sql语句加正则 简化查询
sql语句加正则 简化查询
12 0
sql语句加正则 简化查询
|
26天前
|
SQL
sql server链接查询
sql server链接查询
17 1
|
26天前
|
SQL
sql server简单查询
sql server简单查询
14 1
|
16天前
|
SQL 关系型数据库 MySQL
mysql一条sql查询出多个统计结果
mysql一条sql查询出多个统计结果
12 0
|
26天前
|
SQL
sql高级查询
sql高级查询
12 0
|
1月前
|
SQL 存储 数据可视化
10个高级的 SQL 查询技巧
10个高级的 SQL 查询技巧
|
27天前
|
SQL 数据库
sql server高级查询,看这篇文章就够了
sql server高级查询,看这篇文章就够了
21 0
|
30天前
|
SQL
T-SQL 语句查询
T-SQL 语句查询
55 0
|
1月前
|
SQL 存储 关系型数据库
sql数据库查询语句大全
sql数据库查询语句大全
|
1月前
|
SQL Oracle 关系型数据库
Oracle系列之八:SQL查询
Oracle系列之八:SQL查询