如何爲波紋管查詢的輸出設置列名?爲多個選擇計數(*)表設置列名稱
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