首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sum()的问题

sum()的问题
EN

Stack Overflow用户
提问于 2011-03-15 06:03:27
回答 2查看 83关注 0票数 0

我有一个sql查询

代码语言:javascript
复制
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

像这样..。

代码语言:javascript
复制
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

我还尝试添加group by,但仍然只返回一行

代码语言:javascript
复制
GROUP BY balance_accounting.accounting_id

编辑:

现在我得到一个错误:列'accounting_id‘不能为空

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2011-03-15 06:08:37

代码语言:javascript
复制
 ...
 LEFT JOIN (
   SELECT accounting_id, SUM(amount) AS  amount_off
   FROM $this->db.balance_accounting balance_accounting 
 ) ON balance_accounting.accounting_id=accounting.id
 ...
票数 0
EN

Stack Overflow用户

发布于 2011-03-15 07:40:36

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5305128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档