用join Mysql分组汇总,多个分组汇总

时间:2018-12-10 08:50:54

标签: mysql join group-by sum inner-join

我想按月找到总和。结果返回0行记录。

    Table 1: aidat
1 Messi
2 Ronaldo
3 Neymar

Table 2: aidat_hareket
12 1(messi) 200         12-12-2018
13 3        150         12-11-2018
14 2        25          10-10-2018
15 1        150         10-12-2018
16 3        125         08-05-2018

I want to result this:
id  name  jan  feb march april may ... oct nov  dec
1  messi   0    0   0      0    0       0   0   350
2  ronaldo 0    0   0      0    0       25  0    0
3  neymar  0    0   0      0    125     0   150  0

它在mysql中返回了0行。我尝试在内部联接中按month(ah.datee) and a.student_id;month(ah.datee), a.student_id;a.student_id;分组。但是结果是一样的。

SELECT o.id as o_id, o.ad, s.ad as s_ad, s.plaka, v.id as veli_id,v.ad as 
veli_ad, v.telefon,s.demo, 
if(month(ah.datee)=1 , ah.collection,0) as jan, 
if(month(ah.datee)=2 , ah.collection,0) as feb,
if(month(ah.datee)=3 , ah.collection,0) as march,
if(month(ah.datee)=4 , ah.collection,0) as april,
if(month(ah.datee)=5 , ah.collection,0) as may,
if(month(ah.datee)=6 , ah.collection,0) as june,
if(month(ah.datee)=7 , ah.collection,0) as july,
if(month(ah.datee)=8 , ah.collection,0) as august,
if(month(ah.datee)=9 , ah.collection,0) as sep,
if(month(ah.datee)=10, ah.collection,0) as oct,
if(month(ah.datee)=11, ah.collection,0) as nov,
if(month(ah.datee)=12, ah.collection,0) as december
              from aidat a
              inner join (select student_id, sum(collection_tutari) as collection, collection_datee as datee from aidat_hareket )
                            ah on ah.student_id = a.student_id
              inner join student o on o.id = a.student_id
              inner join service s on s.id = o.service_id
              inner join service_firma sf on sf.id = s.firma_id
              left join aile al on al.student_id = a.student_id
              left join veli v on v.id = al.veli_id
              inner join aylar ay on ay.id = month(ah.datee)
              where a.aylik_odeme_tutari != 0 and sf.id = 23
              group by  month(ah.datee), a.student_id;

我该如何解决?

0 个答案:

没有答案