mysql中sum的总和

发布于 2024-10-24 19:59:20 字数 785 浏览 3 评论 0原文

 SELECT 
f.nummer AS factuur_id,
f.mvoor AS factuur,
f.faktuurnr AS factuur_nr,
f.topdatum AS factuur_datum,
f.vervaldag AS factuur_vervaldatum,
f.totbruto AS factuur_totaal,
SUM(c.totbruto) AS credit_totaal,
SUM(b.bedrag) AS bedrag_totaal,
f.totbruto - (SUM(c.totbruto) + SUM(b.bedrag)) AS SOM

FROM    ((facturen AS f LEFT JOIN creditnota AS c ON f.nummer = c.nummer)

LEFT JOIN betaling1 AS b ON f.nummer = b.factuurnr)

LEFT JOIN klanten AS k ON f.klantnr = k.nr

WHERE f.betaald = 'N'

AND CURDATE() >= DATE_ADD(f.vervaldag, INTERVAL 15 DAY)

GROUP BY f.nummer

HAVING  (factuur_totaal - (SUM(c.totbruto) + SUM(b.bedrag))) > 0 

ORDER BY k.naam

现在我想要 SOM 的 SUM 我需要子查询吗? 我该怎么做?

我知道我可以循环它,但我希望它在 1 个查询中。

mysql,php

谢谢!

 SELECT 
f.nummer AS factuur_id,
f.mvoor AS factuur,
f.faktuurnr AS factuur_nr,
f.topdatum AS factuur_datum,
f.vervaldag AS factuur_vervaldatum,
f.totbruto AS factuur_totaal,
SUM(c.totbruto) AS credit_totaal,
SUM(b.bedrag) AS bedrag_totaal,
f.totbruto - (SUM(c.totbruto) + SUM(b.bedrag)) AS SOM

FROM    ((facturen AS f LEFT JOIN creditnota AS c ON f.nummer = c.nummer)

LEFT JOIN betaling1 AS b ON f.nummer = b.factuurnr)

LEFT JOIN klanten AS k ON f.klantnr = k.nr

WHERE f.betaald = 'N'

AND CURDATE() >= DATE_ADD(f.vervaldag, INTERVAL 15 DAY)

GROUP BY f.nummer

HAVING  (factuur_totaal - (SUM(c.totbruto) + SUM(b.bedrag))) > 0 

ORDER BY k.naam

And now I want a SUM from the SOM
Do I need a subquery for this?
And how to I do this?

I know I can just loop it but I want it in 1 query.

mysql, php

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挥剑断情 2024-10-31 19:59:20

您可以这样做:

SELECT SUM(SOM) FROM (
    SELECT 
    f.nummer AS factuur_id,
    f.mvoor AS factuur,
    f.faktuurnr AS factuur_nr,
    f.topdatum AS factuur_datum,
    f.vervaldag AS factuur_vervaldatum,
    f.totbruto AS factuur_totaal,
    SUM(c.totbruto) AS credit_totaal,
    SUM(b.bedrag) AS bedrag_totaal,
    f.totbruto - (SUM(c.totbruto) + SUM(b.bedrag)) AS SOM

FROM    ((facturen AS f LEFT JOIN creditnota AS c ON f.nummer = c.nummer)
LEFT JOIN betaling1 AS b ON f.nummer = b.factuurnr)
LEFT JOIN klanten AS k ON f.klantnr = k.nr
WHERE f.betaald = 'N'
AND CURDATE() >= DATE_ADD(f.vervaldag, INTERVAL 15 DAY)
GROUP BY f.nummer
HAVING  (factuur_totaal - (SUM(c.totbruto) + SUM(b.bedrag))) > 0 
ORDER BY k.naam) AS temp

基本上,我刚刚将您的查询包装在 SELECT SUM() FROM () AS temp 中。

You can do this:

SELECT SUM(SOM) FROM (
    SELECT 
    f.nummer AS factuur_id,
    f.mvoor AS factuur,
    f.faktuurnr AS factuur_nr,
    f.topdatum AS factuur_datum,
    f.vervaldag AS factuur_vervaldatum,
    f.totbruto AS factuur_totaal,
    SUM(c.totbruto) AS credit_totaal,
    SUM(b.bedrag) AS bedrag_totaal,
    f.totbruto - (SUM(c.totbruto) + SUM(b.bedrag)) AS SOM

FROM    ((facturen AS f LEFT JOIN creditnota AS c ON f.nummer = c.nummer)
LEFT JOIN betaling1 AS b ON f.nummer = b.factuurnr)
LEFT JOIN klanten AS k ON f.klantnr = k.nr
WHERE f.betaald = 'N'
AND CURDATE() >= DATE_ADD(f.vervaldag, INTERVAL 15 DAY)
GROUP BY f.nummer
HAVING  (factuur_totaal - (SUM(c.totbruto) + SUM(b.bedrag))) > 0 
ORDER BY k.naam) AS temp

Basically I've just wrapped your query in a SELECT SUM(<some row>) FROM (<your query>) AS temp.

鹊巢 2024-10-31 19:59:20

SOM 语句包装在另一个 SUM() 调用中:

SELECT ...
    SUM(f.totbruto - (SUM(c.totbruto) + SUM(b.bedrag))) AS SOM
...

Wrap the SOM statement in another SUM() call:

SELECT ...
    SUM(f.totbruto - (SUM(c.totbruto) + SUM(b.bedrag))) AS SOM
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文