2017-04-25 60 views

回答

5

使用as

select ((select count(*) from t_table1 where id = 1) + 
     (select count(*) from t_table2 where id = 1) + 
     (select count(*) from t_table3 where id = 1) 
     ) as col 

请注意,我把括号整个表达式。这不是必需的,但它使代码更具可读性。我也修复了子查询。

如果你想这个多次运行,那么相关子查询使得它更易于管理的ID:

select ((select count(*) from t_table1 t where t.id = x.id) + 
     (select count(*) from t_table2 t where t.id = x.id) + 
     (select count(*) from t_table3 t where t.id = x.id) 
     ) as col 
from (select 1 as id) x; 

然后,修改查询,你只需要在一个地方更改值。

+0

工作很好,谢谢 – antoniogbn

0

使用关键字

select (select count(*) from t_table1 id = 1)+ 
(select count(*) from t_table2 id = 1)+ 
(select count(*) from t_table3 id = 1) as result 
0
select sum(count_tab) as col_name from(
(select count(*) as count_tab from t_table1 id = 1) 
union all 
(select count(*) from t_table2 id = 1) 
union all 
(select count(*) from t_table3 id = 1))