如何为bellow查询的输出设置列名?
select
(select count(*) from t_table1 id = 1)
+
(select count(*) from t_table2 id = 1)
+
(select count(*) from t_table3 id = 1)
答案 0 :(得分: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;
然后,要修改查询,您只需要在一个地方更改值。
答案 1 :(得分: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
答案 2 :(得分: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))