我有两个表,我想找到两个表记录的总和以及每个表记录的总和。这是我的查询,它正确地给出了两个表的总和。
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
最佳答案
尝试如下所示:
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带有样本值
关于mysql - 每个表的总和和单个表的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21572801/