在每个表中有许多行数量。像这样,我在每张表中都有不同数量的金额。每个表的字段名称都不同。如何在单个查询中获取四个表中所有值的总和?有什么办法吗?
答案 0 :(得分:2)
你尝试过这样的事吗?
select sum(val) from (
select sum(col1) as val from table1 where criteria1 = 'x'
union all
select sum(col2) from table2 where criteria2 = 'y'
union all
select sum(col3) from table3 where criteria3 = 'z'
union all
select sum(col4) from table4 where criteria4 = 'w'
) newTbl
答案 1 :(得分:0)
使用派生表 - 你可能想做这样的事情,所以你把所有结果都放在一行!
select
u.user_count,
c.country_count
from
(
select count(*) as user_count from users where username like 'f%'
) u
join
(
select count(*) as country_count from country
) c;
更复杂的例子:http://pastie.org/921407