user3244721
user3244721

Reputation: 754

Sum of each table and Sum of Individual table

I have two table and i want to find sum of both tables records and sum of each also.Here is my query which give me sum of both correctly.

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

This give me tot_live = 300

Now i want to show count of each like 300=100 and 200

tot_live | table1   |table2
300        100        200

Upvotes: 0

Views: 62

Answers (1)

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16086

Try somethine like below:

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

Created SQL FIDDLE with sample values

Upvotes: 1

Related Questions