MySQL----查询同一个表同一字段不同类型

求和(函数判断)1: 查询同一个表同一字段不同类型重点内容

select a.education_sn,
    SUM(if(c.pay_type = 0,c.pay_fee,0)) as educ_fee,
    SUM(if(c.pay_type = 1,c.pay_fee,0)) as filing_fee,
    SUM(if(c.pay_type = 2,c.pay_fee,0)) as collect_fee,
    SUM(if(c.pay_type = 3,c.pay_fee,0)) as guidance_fee
from ah_education as a 
JOIN ah_pay as c on a.eid = c.eid where a.is_delete = 0 and a.eid = 107 GROUP by a.education_sn;

求和(原始)2: 查询同一个表同一字段不同类型

select a.education_sn,
    ( select SUM(pay_fee) from ah_pay where pay_type = 0 and eid = 107 ) as educ_fee ,
    ( select SUM(pay_fee) from ah_pay where pay_type = 1 and eid = 107 ) as filing_fee ,
    ( select SUM(pay_fee) from ah_pay where pay_type = 2 and eid = 107 ) as collect_fee ,
    ( select SUM(pay_fee) from ah_pay where pay_type = 3 and eid = 107 ) as guidance_fee 
from ah_education as a 
JOIN ah_pay as c on a.eid = c.eid where a.is_delete = 0 and a.eid = 107 GROUP by a.education_sn

你可能感兴趣的:(DB)