sql求助,求两张表的并集
想实现以下结果,求大神帮帮表A记录如下:aIDaNum1a200501112a200501123a200501134a200501145a20050115表B记录如下:b...
想实现以下结果,求大神帮帮
表A记录如下:
aID aNum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115
表B记录如下:
bID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408
--------------------------------------------
结果如下:
aID/bID aNum bName
1 a20050111 2006032401
2 a20050112 2006032402
3 a20050113 2006032403
4 a20050114 2006032404
5 a20050115 NULL
8 NULL 2006032408 展开
表A记录如下:
aID aNum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115
表B记录如下:
bID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408
--------------------------------------------
结果如下:
aID/bID aNum bName
1 a20050111 2006032401
2 a20050112 2006032402
3 a20050113 2006032403
4 a20050114 2006032404
5 a20050115 NULL
8 NULL 2006032408 展开
1个回答
展开全部
select case when a.aid is null then b.bid when b.bid is null then a.aid else null end as id,aNum,bName
from a full join b on a.aid=b.bid
from a full join b on a.aid=b.bid
追问
不好意思,忘了说了,我用的是mysql,好像不支持full join,还能有什么好的办法吗?
追答
那改为LEFT JOIN + UNION + RIGHT JOIN 的方式 来实现
select case when a.aid is null then b.bid when b.bid is null then a.aid else null end as id,aNum,bName
from a left join b on a.aid=b.bid
union
select case when a.aid is null then b.bid when b.bid is null then a.aid else null end as id,aNum,bName
from a rightjoin b on a.aid=b.bid
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询