如何将来自不同子查询的多个 SUM 添加到一个结果中?

发布于 2024-09-17 09:07:13 字数 492 浏览 4 评论 0原文

如何将 MySQL 中不同子查询的 SUM 相加?

加入( SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes, customer_id FROM Product1_quote GROUP BY customer_id ) p1q ON (p1q.customer_id = c.customer_id)

加入 ( SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes, customer_id FROM Product2_quote GROUP BY customer_id ) p1q ON (p1q.customer_id = c.customer_id)

所以我想将这两个相加,让 numQuotes 成为总 numQuotes。然而,它比这更复杂一点,因为不同表的数量是动态的,所以在任何给定的情况下都可能有任意数量的子查询。

How can I add up SUMs from different subqueries in MySQL?

JOIN (
SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes, customer_id FROM product1_quote GROUP BY customer_id
) p1q ON (p1q.customer_id = c.customer_id)

JOIN (
SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes, customer_id FROM product2_quote GROUP BY customer_id
) p1q ON (p1q.customer_id = c.customer_id)

So I'd want to add those two up and have numQuotes be the total numQuotes. However, it's a little more complicated than that, because the number of different tables is dynamic, so in any given circumstance there could be any number of subqueries.

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

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

发布评论

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

评论(2

梦亿 2024-09-24 09:07:13

下面的结果是什么?

select sum(numQuotes), customer_id from
(
  (SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes, customer_id FROM 
  product1_quote GROUP BY customer_id ) p1q ON (p1q.customer_id = c.customer_id)
UNION
  (SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes, customer_id FROM 
  product2_quote GROUP BY customer_id ) p1q ON (p1q.customer_id = c.customer_id)
) group by customer_id;

括号可能会被关闭,因此请先检查它们。

What comes up with the following?

select sum(numQuotes), customer_id from
(
  (SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes, customer_id FROM 
  product1_quote GROUP BY customer_id ) p1q ON (p1q.customer_id = c.customer_id)
UNION
  (SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes, customer_id FROM 
  product2_quote GROUP BY customer_id ) p1q ON (p1q.customer_id = c.customer_id)
) group by customer_id;

Parentheses might be off so check them first.

寂寞花火° 2024-09-24 09:07:13

我通过将 JOIN 更改为 LEFT JOIN 并在将查询放在一起的 PHP 循环内使用 IFNULL(numQuotes".$k.",0)+ 解决了这个问题,其中 $k 是一个索引。

因此最终结果类似于:

SELECT IFNULL(numQuotes0,0)+IFNULL(numQuotes1,0) AS totalQuotes FROM ...

LEFT JOIN ( SELECT SUM(IF(isPurchased='0) ', 1, 0)) AS numQuotes0, customer_id FROM Product1_quote GROUP BY customer_id ) p1q ON (p1q.customer_id = c.customer_id)
LEFT JOIN ( SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes1, customer_id FROM Product2_quote GROUP BY customer_id ) p2q ON (p2q.customer_id = c.customer_id)

LEFT JOIN 返回 NULL 如果未找到任何结果,因此需要 IFNULL

I've solved it by changing the JOINs to LEFT JOINs and using IFNULL(numQuotes".$k.",0)+ inside the PHP loop that puts the queries together, where $k is an index.

So the end result is something like:

SELECT IFNULL(numQuotes0,0)+IFNULL(numQuotes1,0) AS totalQuotes FROM ...

LEFT JOIN ( SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes0, customer_id FROM product1_quote GROUP BY customer_id ) p1q ON (p1q.customer_id = c.customer_id)
LEFT JOIN ( SELECT SUM(IF(isPurchased='0', 1, 0)) AS numQuotes1, customer_id FROM product2_quote GROUP BY customer_id ) p2q ON (p2q.customer_id = c.customer_id)

The LEFT JOINs return NULL if no results are found, hence the need for IFNULL.

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