加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

如何将两个sql查询合并为一个

发布时间:2020-12-12 08:40:48 所属栏目:MsSql教程 来源:网络整理
导读:如何组合这两个SQL语句? SELECT SUM(hits01 + hits02 + hits03 + hits04 + hits05 + hits06 + hits07 + hits08 + hits09) AS 'AEROwiz'FROM tbl_2011WHERE appName='AEROwiz'SELECT SUM(hits10 + hits11 + hits12) AS 'AEROwiz'FROM tbl_2010WHERE appName='
如何组合这两个SQL语句?
SELECT SUM(hits01 + hits02 + hits03 + hits04 + hits05 + hits06 + hits07 + hits08 + hits09) AS 'AEROwiz'
FROM tbl_2011
WHERE appName='AEROwiz'

SELECT SUM(hits10 + hits11 + hits12) AS 'AEROwiz'
FROM tbl_2010
WHERE appName='AEROwiz'

hits10,hits11和hits12存在于两个表中.

解决方法

使用UNION查询 – 只需在两个查询之间填充“UNION”:
SELECT SUM(...) AS AEROWiz
FROM ...

UNION

SELECT SUM(...) AS AEROWiz
FROM ...

更新

在另一个查询中包装union:

SELECT SUM(AEROWiz)
FROM (
    .... unioned queries here
) AS child

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读