第一个表(名为18_8_ChartOfAccounts
),帐号如下
AccountNumber | VatReturnRowNumberForDebitTurnover
--------------------------------------------------
1 | not_blank
2 |
3 | not_blank
第二张表(命名2_1_journal
)像这样
Amount | DebitAccount
----------------------
5 | 1
80 | 2
3 | 3
4 | 1
20 | 3
试图获得以下信息:如果VatReturnRowNumberForDebitTurnover
不为空/不为空,则 sum Amount
whereDebitAccount
与 (equal to) 相同AccountNumber
。
或者想要得到这样的输出:
Account number | Total
---------------------------
1 | 9 (5+4)
3 | 23 (20+3)
不需要为 account 回显 Total 2
,因为 account2
VatReturnRowNumberForDebitTurnover
是空白/空的
这个 mysql 查询从第一个表中选择所有 AccountNumber
s ( 18_8_ChartOfAccounts
)
SELECT a.AccountNumber, IFNULL( d.Amount, 0 ) AS Amount
FROM 18_8_ChartOfAccounts AS a
LEFT JOIN ( SELECT DebitAccount, SUM( Amount ) AS Amount
FROM ( SELECT * FROM 2_1_journal) DATA
GROUP BY DebitAccount )
d ON (a.AccountNumber = d.DebitAccount)
但是只需要选择不是空白/空AccountNumber
的sVatReturnRowNumberForDebitTurnover
尝试修改mysql查询
SELECT a.AccountNumber, IFNULL( d.Amount, 0 ) AS Amount
FROM 18_8_ChartOfAccounts AS a
WHERE VatReturnRowNumberForDebitTurnover <> ''
LEFT JOIN ( SELECT DebitAccount, SUM( Amount ) AS Amount
FROM ( SELECT * FROM 2_1_journal) DATA
GROUP BY DebitAccount )
d ON (a.AccountNumber = d.DebitAccount)
得到Syntax error or access violation: ... near 'LEFT JOIN ( SELECT DebitAccount ....
然后变了FROM 18_8_ChartOfAccounts AS a WHERE VatReturnRowNumberForDebitTurnover <> ''
并FROM 18_8_ChartOfAccounts WHERE VatReturnRowNumberForDebitTurnover <> '' AS a
得到类似的错误。
然后改成这样的代码:
SELECT a.AccountNumber, IFNULL( d.Amount, 0 ) AS Amount
FROM 18_8_ChartOfAccounts AS a
LEFT JOIN ( SELECT DebitAccount, SUM( Amount ) AS Amount
FROM ( SELECT * FROM 2_1_journal) DATA
WHERE a.VatReturnRowNumberForDebitTurnover <> ""
GROUP BY DebitAccount )
d ON (a.AccountNumber = d.DebitAccount)
并得到Unknown column 'a.VatReturnRowNumberForDebitTurnover'
好的,在表中2_1_journal
没有这样的列。希望以a.VatReturnRowNumberForDebitTurnover
相同的方式使用a.AccountNumber
(两列仅在18_8_ChartOfAccounts
.a.AccountNumber
作品中,a.VatReturnRowNumberForDebitTurnover
不是。
请告知需要修改的内容