当前位置:   article > 正文

SQL server 多表连接二(采用四种方法)_sql join on 多表连接

sql join on 多表连接

一、表格信息:

二、题目及解答:

1.检索出和职工E1、E3都有联系的北京的供应商信息

法一:full join on连接

  1. select 供应商.供应商号,供应商名,地址
  2. from 供应商 full join 订购单
  3. on 供应商.供应商号=订购单.供应商号
  4. where 职工号='E1' and 地址='北京' and 供应商.供应商号
  5. in
  6. (select 供应商号 from 订购单 where 职工号='E3')

法二:in 连接

  1. SELECT * FROM 供应商 WHERE 地址='北京' AND 供应商号 IN
  2. (SELECT 供应商号 FROM 订购单 WHERE 职工号='E1') AND 供应商号 IN
  3. (SELECT 供应商号 FROM 订购单 WHERE 职工号 ='E3')

法三: exists 连接

  1. select * from 供应商 where 地址='北京' and exists (
  2. select * from 订购单 where 订购单.供应商号=供应商.供应商号 and 职工号='E1') and exists (
  3. select * from 订购单 where 订购单.供应商号=供应商.供应商号 and 职工号='E3')

法四:自然连接

  1. select 供应商.供应商号,供应商名,地址
  2. from 供应商,订购单 where 供应商.供应商号=订购单.供应商号 and 职工号='E1'
  3. and 地址='北京'
  4. intersect
  5. select 供应商.供应商号,供应商名,地址
  6. from 供应商,订购单 where 供应商.供应商号=订购单.供应商号 and 职工号='E3'
  7. and 地址='北京'

 为了快速写入我就直接上代码段啦

2.检索出和面积最小的仓库有联系的供应商的个数

  1. --in连接
  2. select COUNT(*) as 供应商个数 from 供应商
  3. where 供应商号 in(select 供应商号 from 订购单
  4. where 职工号 in(select 职工号 from 职工
  5. where 仓库号 in(select 仓库号 from 仓库
  6. where 面积 in(select min(面积) from 仓库))))
  7. --exists连接
  8. select COUNT(*) as 供应商个数 from 供应商
  9. where exists(select * from 订购单
  10. where 订购单.供应商号=供应商.供应商号 and exists
  11. (select * from 职工 where 订购单.职工号=职工.职工号 and exists
  12. (select * from 仓库 where 仓库.仓库号=职工.职工号 and exists
  13. (select MIN(面积) from 仓库))))
  14. --自然连接
  15. select COUNT(*) as 供应商个数 from 供应商, 订购单,职工,仓库,(select MIN(面积) A from 仓库) as x
  16. where 订购单.供应商号=供应商.供应商号 and 订购单.职工号=职工.职工号 and 仓库.仓库号=职工.职工号 and x.A=仓库.面积
  17. --join连接
  18. select count(*) as 供应商个数
  19. from 供应商 join 订购单 on 订购单.供应商号=供应商.供应商号
  20. join 职工 on 订购单.职工号=职工.职工号 join 仓库 on 仓库.仓库号=职工.职工号
  21. where 面积=(select MIN(面积) from 仓库)

3.  检索出向S4供应商发出订购单的那些仓库的平均面积

  1. --in连接
  2. select AVG(面积) as 平均面积 from 仓库
  3. where 仓库号 in(
  4. select 仓库号 from 职工
  5. where 职工号 in(
  6. select 职工号 from 订购单
  7. where 供应商号='S4') )
  8. --exists连接
  9. select AVG(面积) as 平均面积 from 仓库
  10. where exists(
  11. select * from 职工 where 职工.仓库号=仓库.仓库号
  12. and exists(
  13. select * from 订购单 where 订购单.职工号=职工.职工号 and 供应商号='S4'))
  14. --自然连接
  15. select AVG(distinct(面积)) as 平均面积 from 仓库,职工,订购单
  16. where 仓库.仓库号=职工.仓库号 and 职工.职工号=订购单.职工号
  17. and 供应商号='S4'
  18. --join on 连接
  19. select AVG(distinct(面积)) as 平均面积 from 仓库 join 职工
  20. on 仓库.仓库号=职工.仓库号 join 订购单 on 订购单.职工号=职工.职工号
  21. and 订购单.供应商号='S4'

4.检索出在上海工作并且只向S6供应商发出了订购单的职工号

  1. select 职工.职工号 from 职工,仓库,订购单
  2. where
  3. 职工.仓库号=仓库.仓库号
  4. and 职工.职工号=订购单.职工号
  5. and 城市='上海'
  6. and 供应商号='S6'
  7. group by 职工.职工号
  8. having COUNT(供应商号) =1
  9. select 职工.职工号 from 职工
  10. join 仓库 on 职工.仓库号=仓库.仓库号 and 城市='上海'
  11. join 订购单 on 订购单.职工号=职工.职工号
  12. and 供应商号='S6'
  13. group by 职工.职工号
  14. having COUNT(供应商号) =1
  15. select 职工.职工号 from 职工
  16. where 仓库号 in(
  17. select 仓库号 from 仓库 where 职工号 in(
  18. select 职工号 from 订购单 where 供应商号='S6' and 城市='上海'))
  19. select 职工.职工号 from 职工
  20. where exists(
  21. select * from 仓库 where 仓库.仓库号=职工.仓库号 and exists(
  22. select * from 订购单 where 订购单.职工号=职工.职工号 and 供应商号='S6' and 城市='上海'))
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/189029
推荐阅读
相关标签
  

闽ICP备14008679号