2017-04-25 27 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))