如何为波纹管查询的输出设置列名?为多个选择计数(*)表设置列名称
select
(select count(*) from t_table1 id = 1)
+
(select count(*) from t_table2 id = 1)
+
(select count(*) from t_table3 id = 1)
如何为波纹管查询的输出设置列名?为多个选择计数(*)表设置列名称
select
(select count(*) from t_table1 id = 1)
+
(select count(*) from t_table2 id = 1)
+
(select count(*) from t_table3 id = 1)
使用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;
然后,修改查询,你只需要在一个地方更改值。
工作很好,谢谢 – antoniogbn
使用为关键字
select (select count(*) from t_table1 id = 1)+
(select count(*) from t_table2 id = 1)+
(select count(*) from t_table3 id = 1) as result
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))
你应该看看在SQL'union'和'工会all'。 – kaldoran