如何为波纹管查询的输出设置列名?
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;
然后,要修改查询,只需要在一个位置更改值即可。
关于mysql - 设置多个选择计数(*)表的列名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43610449/