如何检查我的内部联接是对还是错?

时间:2019-07-16 07:36:54

标签: mysql join

select a. App_No, b.App_no, a.Date, Sum(b.RECEIVABLE_AMNT) Prin_Dis_Amount , c.App_no, 
    sum(c.RECEIVABLE_AMNT) Ins_Dis_Amount , d.App_no, sum(d.RECEIVABLE_AMNT) OPE_Dis_Amount 
FROM odk_prod.receivables_test a, odk_prod.receivables_test b, odk_prod.receivables_test c, odk_prod.receivables_test d 
WHERE a.app_no=b.app_no or Null 
    and a.app_no=c.app_no or Null 
    and a.app_no=d.app_no or Null 
    and b.particulars like 'Principal%' 
    and (b.Vouch_name like 'Journal Voucher%' 
        Or b.Vouch_name like 'Disbursement%' 
        Or b.Vouch_name like 'Internal Adjustment%' 
        Or b.Vouch_name like 'Adjustment - Resale%') 
    and c.particulars like 'Disbursement%' 
    and c.vouch_name like 'Insurance - JV%' 
    and d.particulars like 'Disbursement%' 
    and (d.vouch_name like 'Post Sanction OPE%' 
        or d.vouch_name like 'Reimbursement%' 
        or d.vouch_name like 'Fee Debit OPE%' 
        or d.vouch_name like 'Franking%') 
    and a.APP_NO = 'APP-002-066' 
Group By a.App_no

2 个答案:

答案 0 :(得分:0)

  WHERE a.app_no=b.app_no or Null 
     and a.app_no=c.app_no or Null 
     and a.app_no=d.app_no or Null 

将NULL丢掉毫无用处。 如果您尝试查找NULL,则必须采用以下方式:

 app.app IS NULL

答案 1 :(得分:0)

摆脱加入条件中的or Null。如果希望结果包含a中其他表中没有匹配项的行,请使用LEFT JOIN

您不需要与odk_prod.receivables_test进行多次联接。有关如何在不使用自联接的情况下求和不同行类型的信息,请参见need to return two sets of data with two different where clauses

SELECT app_no, date,
    SUM(IF(Vouch_name like 'Journal Voucher%' 
            Or Vouch_name like 'Disbursement%' 
            Or Vouch_name like 'Internal Adjustment%' 
            Or Vouch_name like 'Adjustment - Resale%'), receivable_amnt, 0) AS Prin_Dis_Amount,
    SUM(IF(particulars like 'Disbursement%' 
            and vouch_name like 'Insurance - JV%'), receivable_amnt, 0) AS Ins_Dis_Amount,
    SUM(IF(particulars like 'Disbursement%' 
            AND (vouch_name like 'Post Sanction OPE%' 
                or vouch_name like 'Reimbursement%' 
                or vouch_name like 'Fee Debit OPE%' 
                or vouch_name like 'Franking%'), receivable_amnt, 0) AS OPE_Dis_Amount
FROM odk_prod.receivables_test
GROUP BY app_no