Mysql 为多个select count(*)表设置列名

Mysql 为多个select count(*)表设置列名,mysql,sql,Mysql,Sql,如何为下面查询的输出设置列名 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 where id = 1) + (select count(*) from t_table2 wher

如何为下面查询的输出设置列名

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 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;
然后,要修改查询,只需在一个位置更改值。

使用
作为

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;

然后,要修改查询,只需在一个位置更改值。

使用作为关键字

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 (select count(*) from t_table1 id = 1)+
(select count(*) from t_table2 id = 1)+
(select count(*) from t_table3 id = 1) as result

您应该查看sql中的
union
union all
。您应该查看sql中的
union
union all