每个表的总和和个别表的总和

时间:2014-02-05 09:08:23

标签: mysql count sum

我有两个表,我想找到两个表记录的总和以及每个表的总和。这是我的查询,它给了我两个正确的总和。

select sum(tot_live) as tot_live 
        from ( select count(id) as tot_live from crm_rentals where status = 2 and is_active=1 and is_archive=0
       union
       select count(id) as tot_live from crm_sales where status = 2 and is_active=1 and is_archive=0 ) s

这给我tot_live = 300

现在我想显示每个的计数,如300 = 100和200

tot_live | table1   |table2
300        100        200

1 个答案:

答案 0 :(得分:1)

尝试以下内容:

select @table1:=( select count(id) as tot_live from crm_rentals where status = 2 and is_active=1 and is_archive=0),
@table2:=(select count(id) as tot_live from crm_sales where status = 2 and is_active=1 and is_archive=0 ), 
 (@table1 +@table2) as tot_live

使用示例值

创建SQL FIDDLE