MYSQL UNION JOIN SUM(金额)输出两个结果而不是一个

时间:2017-02-19 13:53:50

标签: mysql join sum union

我有以下内容:

SELECT a.* FROM ( SELECT
      asset_liability_income_expenditure_tbl.a_l_code,
      SUM(mainaccount_a_2017.amount), mainaccount_a_2017.dr_cr_action 
      FROM `mainaccount_a_2017` LEFT JOIN chart_of_account 
      ON (
         chart_of_account.joint_account_numbers =
         mainaccount_a_2017.joint_account_number
       ) 
      LEFT JOIN asset_liability_income_expenditure_tbl 
      ON (
         asset_liability_income_expenditure_tbl.a_l_code =
         chart_of_account.account_type
      ) 
WHERE asset_liability_income_expenditure_tbl.a_l_code = 'FA' 
      AND mainaccount_a_2017.dr_cr_action = 'DR' UNION 
SELECT asset_liability_income_expenditure_tbl.a_l_code,
SUM(mainaccount_b_2017.amount), 
mainaccount_b_2017.dr_cr_action 
FROM `mainaccount_b_2017` 
LEFT JOIN chart_of_account ON (
          chart_of_account.joint_account_numbers = 
          mainaccount_b_2017.joint_account_number
      ) 
LEFT JOIN asset_liability_income_expenditure_tbl ON ( 
           asset_liability_income_expenditure_tbl.a_l_code =
           chart_of_account.account_type
      ) 
WHERE asset_liability_income_expenditure_tbl.a_l_code = 'FA' 
      AND mainaccount_b_2017.dr_cr_action = 'DR' 
) AS a

它工作正常,但在顶部显示一个空行,在下面显示总和或相对于它。我尝试了LIMIT 1,但问题是当第2行的SUM(金额)输出,我无法获取,如果我不应用任何限制,它只获取第1行中SUM(金额)输出的结果。不知道遗失了什么。请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

我明白了。我不得不翻转一些东西。结果如下:

String