【发布时间】:2021-11-04 08:27:49
【问题描述】:
所以我知道我可以通过使用 select count(*) from table1 来获取一个表的计数;
我试过了 选择(从表 1 中选择计数())表 1, (从表 2 中选择计数())表 2 从双;
但它不起作用。
【问题讨论】:
所以我知道我可以通过使用 select count(*) from table1 来获取一个表的计数;
我试过了 选择(从表 1 中选择计数())表 1, (从表 2 中选择计数())表 2 从双;
但它不起作用。
【问题讨论】:
两种可能的解决方案。交叉连接和Union all + 聚合
交叉连接:
select t1.cnt as table1_count,
t2.cnt as table2_count
from
(select count(*) cnt from table1) t1
cross join
(select count(*) cnt from table2) t2
联合所有+最大聚合:
select max(t1_cnt) table1_count, max(t2_cnt) table2_count
from
(
select count(*) t1_cnt, 0 t2_cnt from table1
union all
select 0 t1_cnty, count(*) t2_cnt from table2
) s
【讨论】: