开发者

How to calculate Sum of Count(*) in Mysql

I have two tables that I count rows of them. For example;

Select count(*) FROM tbl_Events

Select count(*) FROM开发者_如何学C tbl_Events2

I need total count. How can I sum the result with a single statement?


select sum(cnt) from (
    select count(*) as cnt from tbl_events
    union all
    select count(*) as cnt from tbl_events2
) as x


Try this:

SELECT (Select count(*) FROM tbl_Events) + (Select count(*) FROM tbl_Events2)

Or (tested in MSSQL), this:

SELECT COUNT(*) 
FROM (SELECT * FROM tbl_Events 
      UNION ALL 
      SELECT * FROM tbl_Events2) AS AllEvents

I'd guess the first will lead to better performance because it has more obvious index options. Test to be sure, though.


Select Count(*)
From(
Select *    From tbl_Events
Union All
Select *    From tbl_Events2) as A
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜