problem with sum()
I have a sql query
开发者_JAVA百科SELECT accounting.id, enclosure.time, enclosure.enc_id_, enclosure.txt, accounting.type_id, accounting.amount, accounting.invoice_id, invoice.invoice_id_, accounting.subaccountoff_id
FROM $this->db.accounting accounting
INNER JOIN $this->db.enclosure enclosure ON enclosure.id=accounting.enc_id
LEFT JOIN $this->db.balance_accounting balance_accounting ON balance_accounting.accounting_id=accounting.id
LEFT JOIN $this->db.invoice invoice ON invoice.id=accounting.invoice_id
LEFT JOIN $this->db.book_enclosure book_enclosure ON book_enclosure.enc_id=enclosure.id
WHERE accounting.group_id='".$_SESSION['gid']."' && (accounting.subaccount_id='$id' || accounting.subaccountoff_id='$id') && accounting.type_id <= 4 && book_enclosure.enc_id IS NULL
ORDER BY enclosure.time DESC, enclosure.enc_id_ DESC
When I add a SUM() in the select clause the query only returns one row?!
like this..
SELECT accounting.id, enclosure.time, enclosure.enc_id_, enclosure.txt, accounting.type_id, accounting.amount, accounting.invoice_id, invoice.invoice_id_, accounting.subaccountoff_id, SUM(balance_accounting.amount) AS amount_off
FROM $this->db.accounting accounting
INNER JOIN $this->db.enclosure enclosure ON enclosure.id=accounting.enc_id
LEFT JOIN $this->db.balance_accounting balance_accounting ON balance_accounting.accounting_id=accounting.id
LEFT JOIN $this->db.invoice invoice ON invoice.id=accounting.invoice_id
LEFT JOIN $this->db.book_enclosure book_enclosure ON book_enclosure.enc_id=enclosure.id
WHERE accounting.group_id='".$_SESSION['gid']."' && (accounting.subaccount_id='$id' || accounting.subaccountoff_id='$id') && accounting.type_id <= 4 && book_enclosure.enc_id IS NULL
ORDER BY enclosure.time DESC, enclosure.enc_id_ DESC
I have also tried to add a group by, but still only one row is returned
GROUP BY balance_accounting.accounting_id
EDIT:
now I get an error: Column 'accounting_id' cannot be null
SELECT accounting.id, enclosure.time, enclosure.enc_id_, enclosure.txt, accounting.type_id, accounting.amount, accounting.invoice_id, invoice.invoice_id_, accounting.subaccountoff_id, balance_accounting.amount_off
FROM $this->db.accounting accounting
INNER JOIN $this->db.enclosure enclosure ON enclosure.id=accounting.enc_id
LEFT JOIN (
SELECT accounting_id, SUM(amount) AS amount_off
FROM $this->db.balance_accounting
) balance_accounting ON balance_accounting.accounting_id=accounting.id
LEFT JOIN $this->db.invoice invoice ON invoice.id=accounting.invoice_id
LEFT JOIN $this->db.book_enclosure book_enclosure ON book_enclosure.enc_id=enclosure.id
WHERE accounting.group_id='".$_SESSION['gid']."' && (accounting.subaccount_id='$id' || accounting.subaccountoff_id='$id') && accounting.type_id <= 4 && book_enclosure.enc_id IS NULL
ORDER BY enclosure.time DESC, enclosure.enc_id_ DESC
...
LEFT JOIN (
SELECT accounting_id, SUM(amount) AS amount_off
FROM $this->db.balance_accounting balance_accounting
) ON balance_accounting.accounting_id=accounting.id
...
SELECT accounting.id, enclosure.time, enclosure.enc_id_, enclosure.txt, accounting.type_id, accounting.amount, accounting.invoice_id, invoice.invoice_id_, accounting.subaccountoff_id, IFNULL(SUM(balance_accounting.amount), 0) AS amount_off
FROM $this->db.accounting accounting
INNER JOIN $this->db.enclosure enclosure ON enclosure.id=accounting.enc_id
LEFT JOIN $this->db.balance_accounting balance_accounting ON balance_accounting.accounting_id=accounting.id
LEFT JOIN $this->db.invoice invoice ON invoice.id=accounting.invoice_id
LEFT JOIN $this->db.book_enclosure book_enclosure ON book_enclosure.enc_id=enclosure.id
WHERE accounting.group_id='".$_SESSION['gid']."' && (accounting.subaccount_id='$id' || accounting.subaccountoff_id='$id') && accounting.type_id <= 4 && book_enclosure.enc_id IS NULL
GROUP BY accounting.id
ORDER BY enclosure.time DESC, enclosure.enc_id_ DESC
精彩评论