全连接、左、右连接查询 比较

数据对比-----------——注意事项!

---------------------------------------------------

A表(a1,b1,c1)      B表(a2,b2)
a1   b1   c1       a2    b2
01   数学 95       01    张三
02   语文 90       02    李四
03   英语 80       04    王五

----------------------------------------------------
select A.*,B.* from A 
inner join B on(A.a1=B.a2)
结果是:
a1   b1   c1       a2    b2
01   数学 95       01    张三
02   语文 90       02    李四
-----------------------------------------------------
select A.*,B.* from A 
left outer join B on(A.a1=B.a2)
结果是:
a1   b1   c1       a2    b2
01   数学 95       01    张三
02   语文 90       02    李四
03   英语 80       NULL  NULL
---------------------------------------------------
select A.*,B.* from A 
right outer join B on(A.a1=B.a2)
结果是:
a1   b1   c1       a2    b2
01   数学 95       01    张三
02   语文 90       02    李四
NULL NULL NULL     04    王五
--------------------------------------------------
select A.*,B.* from A 
full outer join B on(A.a1=B.a2)
结果是:
a1   b1   c1       a2    b2
01   数学 95       01    张三
02   语文 90       02    李四
03   英语 80       NULL  NULL
NULL NULL NULL     04    王五

------------------------------------------------------

1.full join on

-----------full 查询两边都查 数据全 已经去重复(isnull(a.tel,b.tel) as tel)
/*
  查出所有数据,导入exel 过滤出一下信息
  共有数据2385条
  公司独有4749条

  商家独有70条

*/
select isnull(a.tel,b.tel) as tel,isnull(a.serviceno ,null) a_serviceno ,isnull(b.serviceno,null) b_serviceno,
isnull(b.begintime,'') b_begintime ,isnull(b.endtime,'') b_endtime,isnull(b.money,0) b_money,
isnull(a.money,'')a_money,isnull(a.city,'')a_city, isnull(a.contextname,'') a_contextname,isnull(a.servicenameno,'')a_servicenameno,
isnull(a.userid,'') a_userid,isnull(a.zhangqi,'')a_zhangqi
from dbo.gxby a full join dbo.newby b on a.tel=b.tel and a.serviceno=b.serviceno 
order by a.tel;

select g.* from gxby g;-----商家  查* 2451条  
select n.* from newdb n;----公司 总计 22999条
select distinct g.tel from gxby g;----查g.tel不重复 2345条
select distinct n.tel from newdb n;-- 查n.tel不重复 7853条

 ----两表共有述据 2385行记录 
select * from dbo.newby n ,dbo.gxby g  
where n.tel=g.tel and n.serviceno=g.serviceno 
order by g.tel;
2.not in 函数
----------包月商家独有的-----------68条 ?少2个 应该是70条  
------为什么少2条? gx.tel  数据有2条相同的
select gx.* from dbo.gxby gx 
where  gx.tel not in (
select  g.tel from dbo.newby n ,dbo.gxby g  
where n.tel=g.tel and n.serviceno=g.serviceno 
 )order by gx.tel;

-----------公司独有 -------------4749 条 与 full join on 查出来一样
------为什么相同? nb.tel  数据没有相同的
select nb.* from dbo.newby nb
where nb.tel not in (
select n.tel from dbo.newby n ,dbo.gxby g 
where n.tel=g.tel and n.serviceno=g.serviceno 
)order by nb.tel ;

3.left join  on 和 right join on 


--left查询 按照左表的查询  少右表的数据
select isnull(a.tel,0) a_tel,isnull(b.tel,0) b_tel,isnull(a.serviceno ,null) a_serviceno ,isnull(b.serviceno,null) b_serviceno
from dbo.gxby a left join dbo.newby b  on a.tel=b.tel and a.serviceno=b.serviceno ;


--right查询 按照右表的查询 少左表的数据
select isnull(a.tel,b.tel)tel ,isnull(a.serviceno ,null) a_serviceno ,isnull(b.serviceno,null) b_serviceno
from dbo.gxby a right join dbo.newby b on a.tel=b.tel and a.serviceno=b.serviceno ;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值