0
点赞
收藏
分享

微信扫一扫

SQL server 多表连接(采用四种方法)

12a597c01003 2022-04-21 阅读 76

 

1.检索出向供应商S3发过订购单的职工的职工号和仓库号

①自然连接

select 职工.职工号,职工.仓库号 from 职工,订购单
where 职工.职工号=订购单.职工号 and 供应商号='S3'

②in 连接

select 职工号,仓库号 from 职工 where 职工号 in
(select  职工号  from 订购单 where 供应商号='S3'

③join on连接

select 订购单.职工号,仓库号 from 职工 join 订购单 
on 订购单.职工号=职工.职工号
where 供应商号='S3'

④exists连接

select 职工号,仓库号 from 职工 where 
exists(
select *from 订购单 where 职工.职工号=订购单.职工号 and 供应商号='S3'

2.检索出目前与S3供应商没有联系的职工信息

①自然连接

select distinct 职工.职工号,仓库号,工资 from 职工,订购单
 where 职工.职工号=订购单.职工号 and 订购单.供应商号 !='S3'

②in 连接

SELECT  * FROM 职工
WHERE 职工号  not in (SELECT 职工号 FROM 订购单 WHERE 供应商号='S3' AND 订购单.职工号 is not null)

③join on连接

select distinct 仓库号,工资,职工.职工号 from 职工  join 订购单 
 on 订购单.职工号=职工.职工号 where 供应商号 !='S3'

④exists连接

select * from 职工
 where exists(
 select * from 订购单 where 订购单.职工号=职工.职工号 and 订购单.供应商号 !='S3')
举报

相关推荐

0 条评论