无法在mysql查询中获得左外连接结果

时间:2013-06-24 14:25:56

标签: mysql mysql-workbench

  SELECT 
        BB.NAME BranchName,
        VI.NAME Village,
        COUNT(BAC.CBSACCOUNTNUMBER) 'No.Of Accounts',
        SUM(BAC.CURRENTBALANCE) SumOfAmount,
        SUM(CASE
            WHEN transactiontype = 'C' THEN amount
            ELSE 0
        END) AS CreditTotal,
        SUM(CASE
            WHEN transactiontype = 'D' THEN amount
            ELSE 0
        END) AS DebitTotal,
        SUM(CASE
            WHEN transactiontype = 'C' THEN amount
            WHEN transactiontype = 'D' THEN - 1 * amount
            ELSE 0
        END) AS CurrentBalance
    FROM
        CUSTOMER CU,
        APPLICANT AP,
        ADDRESS AD,
        VILLAGE VI,
        BANKBRANCH BB,
        BANKACCOUNT BAC
            LEFT OUTER JOIN
        accounttransaction ACT ON BAC.CBSACCOUNTNUMBER = ACT.BANKACCOUNT_CBSACCOUNTNUMBER
            AND ACT.TRANDATE <= '2013-03-21'
            AND BAC.ACCOUNTOPENINGDATE < '2013-03-21'
            AND ACT.BANKACCOUNT_CBSACCOUNTNUMBER IS NOT NULL
    WHERE
        CU.CODE = AP.CUSTOMER_CODE
            AND BAC.ENTITY = 'CUSTOMER'
            AND BAC.ENTITYCODE = CU.CODE
            AND AD.ENTITY = 'APPLICANT'
            AND AD.ENTITYCODE = AP.CODE
            AND AD.VILLAGE_CODE = VI.CODE
            AND AD.STATE_CODE = VI.STATE_CODE
            AND AD.DISTRICT_CODE = VI.DISTRICT_CODE
            AND AD.BLOCK_CODE = VI.BLOCK_CODE
            AND AD.PANCHAYAT_CODE = VI.PANCHAYAT_CODE
            AND CU.BANKBRANCH_CODE = BB.CODE
            AND BAC.CBSACCOUNTNUMBER IS NOT NULL
            AND ACT.TRANSACTIONTYPE IS NOT NULL
    GROUP BY BB.NAME , VI.NAME;

这是我的信息     我有两个表bankaccount和accountransactions表     如果帐户被创建,它将转到bankaccount表,如果任何交易完成,所以在accounttrasactiosns表中的相应帐号记录但是我想显示相应的分行总帐号的数量,该帐号存在于bankaccount中,它可能是或者在accounttransactions表中可能没有。

1 个答案:

答案 0 :(得分:0)

我猜您遇到的问题是,即使您使用的是accounttransaction,也无法获得LEFT JOIN表中没有数据的帐户的结果。如果是这样,原因是因为您的加入条件包含AND ACT.BANKACCOUNT_CBSACCOUNTNUMBER IS NOT NULL,这会使LEFT JOIN失败。您在WHERE条款中也有两个条件,我打赌不应该在那里。

您应该学习在编码中使用显式连接语法。如果你这样做,你的代码会更加清晰;它将连接条件与WHERE子句分开,这是一个非常不同的事情。我重新编写了你的​​查询作为插图:

SELECT
    BB.NAME BranchName,
    VI.NAME Village,
    COUNT(BAC.CBSACCOUNTNUMBER) 'No.Of Accounts',
    SUM(BAC.CURRENTBALANCE) SumOfAmount,
    SUM(ACT.CurrentBalance) CurrentBalance,
    SUM(ACT.DebitTotal) DebitTotal,
    SUM(ACT.CreditTotal) CreditTotal

FROM   CUSTOMER CU

JOIN   APPLICANT AP
ON     AP.CUSTOMER_CODE = CU.CODE

JOIN   ADDRESS AD
ON     AD.ENTITYCODE = AP.CODE

JOIN   VILLAGE VI
ON     VI.CODE           = AD.VILLAGE_CODE
   AND VI.STATE_CODE     = AD.STATE_CODE
   AND VI.DISTRICT_CODE  = AD.DISTRICT_CODE
   AND VI.BLOCK_CODE     = AD.BLOCK_CODE
   AND VI.PANCHAYAT_CODE = AD.PANCHAYAT_CODE

JOIN   BANKBRANCH BB
ON     BB.CODE = CU.BANKBRANCH_CODE

JOIN   BANKACCOUNT BAC
ON     BAC.ENTITYCODE = CU.CODE

LEFT OUTER JOIN (
   SELECT BANKACCOUNT_CBSACCOUNTNUMBER,
          SUM(CASE
             WHEN transactiontype = 'C' THEN amount
             ELSE 0
             END) AS CreditTotal,
          SUM(CASE
             WHEN transactiontype = 'D' THEN amount
             ELSE 0
             END) AS DebitTotal,
          SUM(CASE
             WHEN transactiontype = 'C' THEN amount
             WHEN transactiontype = 'D' THEN - 1 * amount
             ELSE 0
             END) AS CurrentBalance
   FROM    accounttransaction
   WHERE   TRANDATE <= '2013-03-21'
   GROUP BY BANKACCOUNT_CBSACCOUNTNUMBER
   ) ACT

ON     ACT.BANKACCOUNT_CBSACCOUNTNUMBER = BAC.CBSACCOUNTNUMBER
   AND BAC.ACCOUNTOPENINGDATE < '2013-03-21'

WHERE  BAC.ENTITY = 'CUSTOMER'
   AND AD.ENTITY = 'APPLICANT'

GROUP BY BB.NAME , VI.NAME;

我从LEFT JOIN条件

中删除了这一行
AND ACT.BANKACCOUNT_CBSACCOUNTNUMBER IS NOT NULL

我从WHERE子句中删除了这两行

AND BAC.CBSACCOUNTNUMBER IS NOT NULL
AND ACT.TRANSACTIONTYPE IS NOT NULL

如果这不能解决您的问题,请修改您的问题以进一步解释。

更新:根据评论,修改查询以使用派生表按帐户计算借方,贷方和当前余额。

另请注意左{}}条件的放置BAC.ACCOUNTOPENINGDATE < '2013-03-21'条件。如上所述,无论开放日期如何,这都将返回所有帐户。如果您只想显示在该日期之前打开的帐户,则应将此条件移至WHERE子句。