关于三张表full outer join 的问题,向高手求助!

windforcecn 2009-09-02 02:23:08
运行code代码如下

DECLARE @test1 TABLE(
[id] int,
[name] [varchar](18)
)
DECLARE @test2 TABLE(
[id] int,
[name] [varchar](18)
)

DECLARE @test3 TABLE(
[id] int,
[name] [varchar](18)
)


insert into @test1 values(1,'a')
insert into @test1 values(2,'b')
insert into @test1 values(3,'c')


insert into @test2 values(1,'d')
insert into @test2 values(2,'e')
insert into @test2 values(3,'f')
insert into @test2 values(4,'g')
insert into @test2 values(5,'h')


insert into @test3 values(1,'i')
insert into @test3 values(2,'j')
insert into @test3 values(3,'k')
insert into @test3 values(4,'l')

select * from @test1 a full outer join @test2 b on a.id=b.id full outer join @test3 c on a.id=c.id



得到结果是


id name id name id name
1 a 1 d 1 i
2 b 2 e 2 j
3 c 3 f 3 k
NULL NULL 4 g NULL NULL
NULL NULL 5 h NULL NULL
NULL NULL NULL NULL 4 l


这个结果是不对的,因为右边test3表的4 ,1 值应当和第2张test2表的 4平行。

应该是
select * from @test1 a full outer join @test2 b on a.id=b.id full outer join @test3 c on b.id=c.id

结果为

id name id name id name
1 a 1 d 1 i
2 b 2 e 2 j
3 c 3 f 3 k
NULL NULL 4 g 4 1
NULL NULL 5 h NULL NULL


但我并不知道test1,test2,test3 哪张表的数据较多,所以不能知道应该用b的id还是a.id来匹配c的id。

请问有办法吗?请不要告诉我先count出来哪张表数据多。
...全文
926 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
soft_wsx 2009-09-02
  • 打赏
  • 举报
回复
select t1.* , t2.* , t3.* from @test1 t1
full join @test2 t2 on t1.id = t2.id
full join @test3 t3 on t2.id = t3.id
dawugui 2009-09-02
  • 打赏
  • 举报
回复
DECLARE  @test1 TABLE( 
[id] int,
[name] [varchar](18)
)
DECLARE @test2 TABLE(
[id] int,
[name] [varchar](18)
)

DECLARE @test3 TABLE(
[id] int,
[name] [varchar](18)
)


insert into @test1 values(1,'a')
insert into @test1 values(2,'b')
insert into @test1 values(3,'c')


insert into @test2 values(1,'d')
insert into @test2 values(2,'e')
insert into @test2 values(3,'f')
insert into @test2 values(4,'g')
insert into @test2 values(5,'h')


insert into @test3 values(1,'i')
insert into @test3 values(2,'j')
insert into @test3 values(3,'k')
insert into @test3 values(4,'l')

select t1.* , t2.* , t3.* from @test1 t1
full join @test2 t2 on t1.id = t2.id
full join @test3 t3 on t2.id = t3.id

/*
id name id name id name
----------- ------------------ ----------- ------------------ ----------- ------------------
1 a 1 d 1 i
2 b 2 e 2 j
3 c 3 f 3 k
NULL NULL 4 g 4 l
NULL NULL 5 h NULL NULL

(所影响的行数为 5 行)

*/
windforcecn 2009-09-02
  • 打赏
  • 举报
回复
多谢多谢!
windforcecn 2009-09-02
  • 打赏
  • 举报
回复
楼上,我是要join,不是union数据啊
htl258_Tony 2009-09-02
  • 打赏
  • 举报
回复 1
DECLARE  @test1 TABLE( 
[id] int,
[name] [varchar](18)
)
DECLARE @test2 TABLE(
[id] int,
[name] [varchar](18)
)

DECLARE @test3 TABLE(
[id] int,
[name] [varchar](18)
)


insert into @test1 values(1,'a')
insert into @test1 values(2,'b')
insert into @test1 values(3,'c')


insert into @test2 values(1,'d')
insert into @test2 values(2,'e')
insert into @test2 values(3,'f')
insert into @test2 values(4,'g')
insert into @test2 values(5,'h')


insert into @test3 values(1,'i')
insert into @test3 values(2,'j')
insert into @test3 values(3,'k')
insert into @test3 values(4,'l')

select * from @test1 a full outer join @test2 b on a.id=b.id full outer join @test3 c on isnull(a.id,b.id)=c.id
/*
id name id name id name
1 a 1 d 1 i
2 b 2 e 2 j
3 c 3 f 3 k
NULL NULL 4 g 4 l
NULL NULL 5 h NULL NULL
*/
看错.加个ISNULL在条件就行了.
htl258_Tony 2009-09-02
  • 打赏
  • 举报
回复
DECLARE  @test1 TABLE( 
[id] int,
[name] [varchar](18)
)
DECLARE @test2 TABLE(
[id] int,
[name] [varchar](18)
)

DECLARE @test3 TABLE(
[id] int,
[name] [varchar](18)
)


insert into @test1 values(1,'a')
insert into @test1 values(2,'b')
insert into @test1 values(3,'c')


insert into @test2 values(1,'d')
insert into @test2 values(2,'e')
insert into @test2 values(3,'f')
insert into @test2 values(4,'g')
insert into @test2 values(5,'h')


insert into @test3 values(1,'i')
insert into @test3 values(2,'j')
insert into @test3 values(3,'k')
insert into @test3 values(4,'l')

select isnull(isnull(a.id,b.id),c.id) id,isnull(isnull(a.name,b.name),c.name) name
from @test1 a full outer join @test2 b on a.id=b.id full outer join @test3 c on a.id=c.id
/*
id name
1 a
2 b
3 c
4 g
5 h
4 l
*/
不需要开发,0行代码写接口服务,sql编程,只要会sql就会写接口服务,让后端变得更简单,简单4步短短5分钟,立马上手,java小白也可以写接口。订阅课程后可以免费获取发布版进行使用和测试。 0行代码写服务的需要来源案例一,当时有个项目,有400张,都是管理系统,单维护的内容较多,当时的项目团队是13人,前后端都写,那时候还没有springboot,用的是ssm,mybatis刚出来,有了替代hibernate的趋势,ifelse写了一堆又一堆,实体类也是,当时的后端分了7层☒,天天加班干这活,复制粘贴,很容易犯错,实体类多人引用修改,真的是废了很大的劲……案例二,也是一个比较大的项目,两千万多万那种,当时为了拿项目,需要快速实现原型给客户看,要求比较高,虽说是原型但是数据全部需要实时,这时候就需要大量编写数据接口,同样编写接口这件事难度倒是不大,但是量大,编写过程手写很容易出错……案例三,以前管理的团队主要做移动端开发,里面的项目会涉及到推送,管理系统,数据采集与同步,总之很多内容,需要前后端通吃,我不仅需要出方案,设计原型,设计数据库,出报价,沟通需求,还要写后端框架,数据接口与数据采集,开发前端(web端),移动端,管理所有项目,但是那时候招的人只会写移动端,实在是忙不过来,我就想能不能有个框架让不会写java的人能写接口,因为写移动端sqlite总是会用的,也就是说写sql不是难点……基于以上三点需求的积累,我利用业余时间写了一个后端框架,完成了这样的需求,刚开始是需要写三行代码完成一个接口,经过后面优化,现在不写代码也可以实现……  本框架涉及的知识点比较多,目前提供最基础版供大家学习和使用,后期逐步推出框架具体的教程和功能内容,下期我们讲如何在实际项目中通过部署版如何完成所需要的接口编写,欢迎大家订阅。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧