结合2个sql select count语句

时间:2015-06-03 09:03:28

标签: sql jet

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

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

如何最简单的方法来组合他们的结果(总和)?

2 个答案:

答案 0 :(得分:3)

使用UNION ALL获取两个不同的计数:

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

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

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

答案 1 :(得分:2)

取决于你的意思"结合"。总结一下:

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