结合 2 sql select 计数语句

combine 2 sql select count statements

我有 2 个简单的计数查询:

select count (*) from t_object
select count (*) from t_diagram  

如何最简单地合并他们的结果(和)?

取决于您所说的 "combine" 是什么意思。总结一下:

select  (select count (*) from t_object) + count(*) as combined_count
from    t_diagram

使用UNION ALL得到两个不同的计数:

select count (*), 't_object count' from t_object
union all
select count (*), 't_diagram count' from t_diagram

要获得计数总和,请使用派生的 table:

select sum(dt.cnt) from
(
 select count(*) as cnt from t_object
 union all
 select count(*) as cnt from t_diagram
) dt

或者,使用子查询:

select count(*) + (select count(*) from t_diagram) from t_object