22,209
社区成员
发帖
与我相关
我的任务
分享
select id,name,'0' AS ccc from tableA
union
select id,name,'1' AS ccc from tableB
select id,name,max(ccc) ccc from (
select id,name,'0' AS ccc from tableA
union
select id,name,'1' AS ccc from tableB
) temp1
group by id,name
select id,name,'0' AS ccc from tableA
union
select id,name,'0' AS ccc from tableB
这样既有ccc的栏位 有去掉了重复的.
select id,name,'0' AS ccc from tableA
union
select id,name,'1' AS ccc from tableB
where not exisit (select 1 AS ccc from tableA where tableA.name=tableB.name
and tableA.id=tableB.id)
这样的话你看如何select id,name from tableA
union
select id,name from tableB
但如果代码如下,由于'0' AS ccc和'1' AS ccc的存在,导致union无法过滤id,name重复的数据,最终效果就是union等同于union all了
select id,name,'0' AS ccc from tableA
union
select id,name,'1' AS ccc from tableB