使用join mysql在group by中求和,多个group by

ar5n3qh5  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(291)

我想按月份计算总数。结果返回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;

我怎样才能修好它?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题