如果子条目在from子句

时间:2015-05-10 17:09:25

标签: mysql sql subquery

我有两个表trans_transaction和opening_balances。 事务将插入trans_transaction表中。 我需要从trans_transaction表中获取opening_balance,closing_balance。当trans_transaction表中的帐户没有事务时,open_balances表首次使用。 我还编写了一个查询,但如果trans_transaction表中没有记录并且无法从opening_balances表中获取数据,则查询失败

SELECT `aasr`.`account_id`, `aasr`.`sub_account_id`, `aasr`.`opening_balance` `oob`, `aasr`.`shadow_opening_balance` `osb`, `aasr`.`lein_opening_balance` `olb`, `tt`.`opening_balance` `ob`, `tt`.`closing_balance` `cb`, `tt`.`shadow_balance` `sb`, `tt`.`lein_balance` `lb`
FROM `accnt_acc_subacc_rel` `aasr`,
    (
        SELECT `tti`.`transaction_id`, `tti`.`accounts_id`, `tti`.`sub_account_id`, `tti`.`opening_balance`, `tti`.`closing_balance`, `tti`.`shadow_balance`, `tti`.`lein_balance`
        FROM `trans_transaction` `tti`
        WHERE `tti`.`accounts_id` = 10 AND `tti`.`sub_account_id` = 20 AND `tti`.`effective_date` <= '2014-10-12 23:59:59'
        ORDER BY `tti`.`effective_date` DESC, `tti`.`transaction_id` DESC
    ) `tt`
WHERE `aasr`.`account_id` = 10 AND `aasr`.`sub_account_id` = 20
LIMIT 1

我也尝试使用以下查询,但如果SubQuery返回0行

,它仍会失败
SELECT  `aasr`.`account_id`, `aasr`.`sub_account_id`,
            IF(`tt`.`opening_balance` = '', `aasr`.`opening_balance`, `tt`.`opening_balance`) AS `opening_balance`,
            IF(`tt`.`closing_balance` = '', `aasr`.`opening_balance`, `tt`.`closing_balance`) AS `closing_balance`,
            IF(`tt`.`shadow_balance` = '', `aasr`.`shadow_opening_balance`, `tt`.`opening_balance`) AS `shadow_balance`,
            IF(`tt`.`lein_balance` = '', `aasr`.`lein_opening_balance`, `tt`.`lein_balance`) AS `lein_balance`
    FROM `accnt_acc_subacc_rel` `aasr`,
        (
            SELECT `tti`.`transaction_id`, `tti`.`accounts_id`, `tti`.`sub_account_id`, `tti`.`opening_balance`, `tti`.`closing_balance`, `tti`.`shadow_balance`, `tti`.`lein_balance`
            FROM `trans_transaction` `tti`
            WHERE `tti`.`accounts_id` = 10 AND `tti`.`sub_account_id` = 20 AND `tti`.`effective_date` <= '2014-10-12 23:59:59'
            ORDER BY `tti`.`effective_date` DESC, `tti`.`transaction_id` DESC
            LIMIT 1
        ) `tt`
    WHERE `aasr`.`account_id` = 10 AND `aasr`.`sub_account_id` = 20

我没有得到出错的地方......

1 个答案:

答案 0 :(得分:0)

使用&#34; LEFT OUTER JOIN&#34;正如@BaconBits建议和IFNULL一样:

SELECT  `aasr`.`account_id`, `aasr`.`sub_account_id`,
        IFNULL(`tt`.`opening_balance`, `aasr`.`opening_balance`) AS `opening_balance`,
        IFNULL(`tt`.`closing_balance`, `aasr`.`opening_balance`) AS `closing_balance`,
        IFNULL(`tt`.`shadow_balance`, `aasr`.`shadow_opening_balance`) AS `shadow_balance`,
        IFNULL(`tt`.`lein_balance`, `aasr`.`lein_opening_balance`) AS `lein_balance`
FROM `accnt_acc_subacc_rel` `aasr`
LEFT OUTER JOIN
    (
        SELECT `tti`.`transaction_id`, `tti`.`accounts_id`, `tti`.`sub_account_id`, `tti`.`opening_balance`, `tti`.`closing_balance`, `tti`.`shadow_balance`, `tti`.`lein_balance`
        FROM `trans_transaction` `tti`
        WHERE `tti`.`accounts_id` = 10 AND `tti`.`sub_account_id` = 20 AND `tti`.`effective_date` <= '2014-10-12 23:59:59'
        ORDER BY `tti`.`effective_date` DESC, `tti`.`transaction_id` DESC
        LIMIT 1
    ) `tt` 
    ON `aasr`.`accounts_id` = `tt`.`accounts_id` AND `aasr`.`sub_account_id` = `tt`.`sub_account_id` 
WHERE `aasr`.`account_id` = 10 AND `aasr`.`sub_account_id` = 20

(@ Nikhilesh_Manurkar在评论中询问你是否可以在没有子查询的情况下这样做,所以:)

在没有子查询的情况下这样做(小心!!!对于这些WHERE条件,在子查询中首先过滤trans_transaction更有效率):

SELECT  
    `aasr`.`account_id`,
    `aasr`.`sub_account_id`,
    IFNULL(`tt`.`opening_balance`, `aasr`.`opening_balance`) AS `opening_balance`,
    IFNULL(`tt`.`closing_balance`, `aasr`.`opening_balance`) AS `closing_balance`,
    IFNULL(`tt`.`shadow_balance`, `aasr`.`shadow_opening_balance`) AS `shadow_balance`,
    IFNULL(`tt`.`lein_balance`, `aasr`.`lein_opening_balance`) AS `lein_balance`

FROM 
    `accnt_acc_subacc_rel` `aasr`

LEFT OUTER JOIN 
    `trans_transaction` `tt`
    ON 
        `aasr`.`accounts_id` = `tt`.`accounts_id` 
        AND `aasr`.`sub_account_id` = `tt`.`sub_account_id` 

WHERE 
    `aasr`.`account_id` = 10 
    AND `aasr`.`sub_account_id` = 20 
    # case no record in `tt` to match `aasr` join, `tt`.`effective_date` would be null,
    # so you should add this to the "WHERE" condition
    AND ( `tt`.`effective_date` <= '2014-10-12 23:59:59' OR ISNULL `tt`.`effective_date`)

ORDER BY 
    `tt`.`effective_date` DESC, `tt`.`transaction_id` DESC