mysql - 每个表的总和和单个表的总和

标签 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

最佳答案

尝试如下所示:

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/

相关文章:

mysql - 带有 SUM 的 IF 在 MySQL 中产生不正确的结果

mysql - 显示 SQL DB 中的数据

mysql - 将 mysql 查询转换为 laravel 查询

mysql - 如何使用mysql获取总和和子集的总和?

Flutter)如何计算 map 内的特定值

mysql 选择计数联合

MySQL 复杂查询

MySQL alter table 生成 "error on rename"

c# - 使用 SUM 和 ORDER BY 的 Linq 查询

MySQL : How to count multiple row where datediff() is less than 30