Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

mysql kiri gabungkan jumlah dua tabel dengan rollup

Berikut skenario detailnya:

Dengan Data yang Diberikan:

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

hasil:

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

struktur kueri akhir:

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. Mengisi kolom input pilih dengan nilai dari mysql

  2. bug aneh saat membaca dari file csv dan mengimpor data ke mysql menggunakan php

  3. Cara menggunakan dalam klausa di subquery

  4. Bisakah saya mendefinisikan beberapa acara dalam satu deklarasi Pemicu di mysql?

  5. Dokumentasi tidak jelas mysqldb Python