我有两个简单的计数查询:
select count (*) from t_object
select count (*) from t_diagram
如何最简单的方法来组合他们的结果(总和)?
答案 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