消息关闭
    暂无新消息!

Sql union 操作

问题作者 : 姚明2017-06-05发布

问题:
第一种写法:

(select  du.day,du.apptoken ,du.version, du.channel,du.city,du.count,concat(apptoken, 

version,channel,city) as joinkey from day_new_users_count du where day='20170319') as dayUsers 

 union 

(select tu.day,tu.apptoken,tu.version,tu.channel,tu.city,tu.count,concat(apptoken,version,channel,city) as joinkey from total_users tu where day='20170318') as toUsers

第二种写法:


select  du.day,du.apptoken ,du.version, du.channel,du.city,du.count,concat(apptoken, version,channel,city) as joinkey from day_new_users_count du where day='20170319'
 union 

select tu.day,tu.apptoken,tu.version,tu.channel,tu.city,tu.count,concat(apptoken,version,channel,city) as joinkey from total_users tu where day='20170318'

为什么第二种写法可以正确执行,第一种方式就不可以??

区别 不是 第一种方式中给 临时表起了个别名嘛,怎么就不行了?高人指点呐


3个回答

︿ 3
with dayUsers as (select
du.day, du.apptoken , du.version, du.channel, du.city, du.count,
concat(apptoken, version,channel,city) as joinkey
from day_new_users_count du
where day='20170319'
), toUsers as (select
tu.day,  tu.apptoken, tu.version, tu.channel, tu.city, tu.count,
concat(apptoken,version,channel,city) as joinkey
from total_users tu
where day='20170318')
select * from dayUsers union select * from toUsers;
︿ 1

select * from (
select
du.day, du.apptoken , du.version, du.channel, du.city, du.count,
concat(apptoken, version,channel,city) as joinkey
from day_new_users_count du
where day='20170319'
) as dayUsers
union
select * from (
select
tu.day,  tu.apptoken, tu.version, tu.channel, tu.city, tu.count,
concat(apptoken,version,channel,city) as joinkey
from total_users tu
where day='20170318'
) as toUsers
︿ 0

UNION 是用于两个记录集
第一种的写法
(select du.day,du.apptoken ,du.version, du.channel,du.city,du.count,concat(apptoken,

version,channel,city) as joinkey from day_new_users_count du where day='20170319') as dayUsers

不是记录集