Sql union 操作

发布于 2022-09-05 00:11:51 字数 856 浏览 18 评论 0

问题:
第一种写法:

(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'

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

向地狱狂奔 2022-09-12 00:11:51

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
雪花飘飘的天空 2022-09-12 00:11:51

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

不是记录集

原谅我要高飞 2022-09-12 00:11:51
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文