Mysql
 sql >> база данни >  >> RDS >> Mysql

mysql left join сумира две таблици със сборен пакет

Ето сценария с подробности:

С дадени данни:

select a.title_id,  sum(revenue), sum(cost),sum(interest) from
(select a.title_id,  sum(revenue) as revenue, sum(cost) as cost from
(select  1 title_id, 10 revenue , 5 cost UNION all
select 2,   10,  5 UNION all
select 3,   10,  5 UNION all
select 4,   10,  5 UNION all
select 1,   20,  6 UNION all
select 2,   20,  6 UNION all
select 3,   20,  6 UNION all
select 4,   20,  6) as a
GROUP BY title_id) as a

left JOIN

(select title_id, sum(interest) as interest from
(select 1 as title_id, 10 as interest UNION all
select 2,   10 UNION all
select 3,  10 UNION all
select 4,   10 UNION all
select 1,  20 UNION all
select 2,  20 UNION all
select 3, 20 UNION all
select 4,  20) as b
GROUP BY title_id ) as b
on a.title_id = b.title_id 
GROUP BY a.title_id
with ROLLUP

резултат:

1   30  11  30
2   30  11  30
3   30  11  30
4   30  11  30
    120 44  120

окончателна структура на заявката:

select a.title_id,  sum(revenue), sum(cost),sum(interest) from
(select a.title_id,  sum(revenue) as revenue, sum(cost) as cost from
(select * from revenue) as a
GROUP BY title_id) as a

left JOIN

(select title_id, sum(interest) as interest from
(select * from fund) as b
GROUP BY title_id ) as b
on a.title_id = b.title_id 
GROUP BY a.title_id
with ROLLUP



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Генериране на MySQL в Excel с помощта на PHP

  2. MariaDb SQL инжекция

  3. Как да получите размера на байтовете на набора от резултати в SQL заявка?

  4. Създайте масив за PDO от променливи, предадени от jquery

  5. Как да оптимизираме базата данни на уебсайта