2014-02-05 35 views
0

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

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

尝试somethine像下面的计数:

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 [email protected]) as tot_live 

带有样本值的SQL FIDDLE创建

+0

这已经为tot_live工作,我需要每个表的计数也 – user3244721

+0

你甚至尝试过这种解决方案,user3244721? – Martin

+0

它给我这个问题在'字段列表'中的未知列'table2' – user3244721