如何在Mysql中计算Count(*)的总和

时间:2022-03-13 22:29:20

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

我有两个表,我计算它们的行数。例如;

Select count(*) FROM tbl_Events

Select count(*) FROM tbl_Events2

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

我需要总数。如何用单个语句对结果求和?

3 个解决方案

#1


19  

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

#2


3  

Try this:

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

Or (tested in MSSQL), this:

或者(在MSSQL中测试),这个:

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.

我猜第一个会带来更好的性能,因为它有更明显的索引选项。不过要测试一下。

#3


0  

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

#1


19  

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

#2


3  

Try this:

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

Or (tested in MSSQL), this:

或者(在MSSQL中测试),这个:

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.

我猜第一个会带来更好的性能,因为它有更明显的索引选项。不过要测试一下。

#3


0  

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