我有3张桌子彼此没有关系 [英] I have 3 tables there is no relation between each other

查看:99
本文介绍了我有3张桌子彼此没有关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表格是这样的数据



the tables habe the data like this

create table t1 (a1 int,b1 int ,c1 int)
create table t2 (a2 int,b2 int ,c2 int)
create table t3 (a3 int,b3 int ,c3 int)

insert into t1 values(1,5,6),(5,8,4)
insert into t2 values(5,8,9),(1,4,4)
insert into t2 values(6,2,7),(7,6,0)





i希望输出如下





i want the output like below

(c1+c2+c3)
----------
22
8





我的尝试:





What I have tried:

WITH
  b1(a1, b1,c1) AS (select * from t1),
  b2(a2,b2,c2) AS (select * from t2),
  b3(a3,b3,c3)as (select * from t3),
 b4(csjdls) as (( select c1+c2+c3 from b1,b2,b3))
SELECT csjdls
FROM b4





i尝试了以上它当我只有一张唱片只有一张唱片时工作正常

但我想要多张唱片



i tried the above it iw working fine when only table having only one record
but i want to for multiple records

推荐答案

试试这个:

Try this:
SELECT SUM(c) AS TotalC
FROM (
SELECT c1 AS c FROM t1
UNION ALL
SELECT c2 AS c FROM t2
UNION ALL
SELECT c3 AS c FROM t3
) AS t


这篇关于我有3张桌子彼此没有关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆