我有两个表,我计算它们的行数。例如;
Select count(*) FROM tbl_Events
Select count(*) FROM tbl_Events2
我需要总数。如何用单一陈述对结果求和?
答案 0 :(得分:19)
select sum(cnt) from (
select count(*) as cnt from tbl_events
union all
select count(*) as cnt from tbl_events2
) as x
答案 1 :(得分:3)
试试这个:
SELECT (Select count(*) FROM tbl_Events) + (Select count(*) FROM tbl_Events2)
或(在MSSQL中测试),这个:
SELECT COUNT(*)
FROM (SELECT * FROM tbl_Events
UNION ALL
SELECT * FROM tbl_Events2) AS AllEvents
我猜第一个会带来更好的性能,因为它有更明显的索引选项。不过要测试一下。
答案 2 :(得分:0)
Select Count(*)
From(
Select * From tbl_Events
Union All
Select * From tbl_Events2) as A