How to calculate graph sum (*) in Mysql

I have two tables that I count from them. For instance:

Select count(*) FROM tbl_Events

Select count(*) FROM tbl_Events2

I need a general bill. How can I summarize the result with a single statement?

+5
source share
3 answers
select sum(cnt) from (
    select count(*) as cnt from tbl_events
    union all
    select count(*) as cnt from tbl_events2
) as x
+18
source

Try the following:

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

Or (verified in MSSQL), this is:

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

I assume that the former will lead to better performance, since it has more obvious index parameters. Of course, make sure.

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

All Articles