在MySQL中的独特价值和总和其他

发布于 2025-01-18 14:44:29 字数 2159 浏览 0 评论 0原文

我正在尝试获取 unique> unique od_grp_idorder_payment_total一次,但是在使用sum时会添加它。

CREATE TABLE IF NOT EXISTS `subscription` (
  `id` int(11) unsigned NOT NULL, 
  `od_grp_id` int(11) unsigned NULL, 
  `user_id` int(11) NOT NULL, 
  `order_discount` decimal(10, 2) null, 
  PRIMARY KEY (`id`)
) DEFAULT CHARSET = utf8;

INSERT INTO `subscription` (
  `id`, `od_grp_id`, `user_id`, `order_discount`
) 
VALUES 
  (123994, NULL, 115, null), 
  (124255, NULL, 115, null), 
  (124703, 1647692222, 115, null), 
  (125788, 1647692312, 115, '25.00'), 
  (125789, 1647692312, 115, '5.00');
CREATE TABLE IF NOT EXISTS `online_payment_against_subscription` (
  `subscription_od_grp_id` int(11) unsigned NOT NULL, 
  `order_payment_total` decimal(10, 2) unsigned NOT NULL, 
  `user_id` int(11) NOT NULL
) DEFAULT CHARSET = utf8;

INSERT INTO `online_payment_against_subscription` (
  `subscription_od_grp_id`, `order_payment_total`, `user_id` 
) 
VALUES 
  (1643695200, '45.00', 115), 
  (1647692312, '250.00', 115), 
  (1647692222, '30.00', 115);
SELECT 
  sum(y.order_payment_total), 
  sum(s.order_discount) 
FROM 
  subscription s 
  LEFT JOIN(
    SELECT 
      SUM(order_payment_total) as order_payment_total, 
      user_id, 
      subscription_od_grp_id 
    FROM 
      online_payment_against_subscription 
    GROUP BY 
      subscription_od_grp_id
  ) y ON y.subscription_od_grp_id = s.od_grp_id 
WHERE 
  find_in_set(
    s.id, '123994,124255,124703,125788,125789'
  ) 
group by 
  s.user_id
Current Output:
| sum(y.order_payment_total) |sum(s.order_discount)  |
|----------------------------|-----------------------|
|                        530 |                    30 |


Expected Ouput:
| sum(y.order_payment_total) |sum(s.order_discount)  |
|----------------------------|-----------------------|
|                       280  |                    30 |

SQL小提琴: http://sqlfiddle.com/#!9/5628f5/5628f5/1

I am trying to get the order_payment_total of the unique od_grp_id once but while using sum it get added.

CREATE TABLE IF NOT EXISTS `subscription` (
  `id` int(11) unsigned NOT NULL, 
  `od_grp_id` int(11) unsigned NULL, 
  `user_id` int(11) NOT NULL, 
  `order_discount` decimal(10, 2) null, 
  PRIMARY KEY (`id`)
) DEFAULT CHARSET = utf8;

INSERT INTO `subscription` (
  `id`, `od_grp_id`, `user_id`, `order_discount`
) 
VALUES 
  (123994, NULL, 115, null), 
  (124255, NULL, 115, null), 
  (124703, 1647692222, 115, null), 
  (125788, 1647692312, 115, '25.00'), 
  (125789, 1647692312, 115, '5.00');
CREATE TABLE IF NOT EXISTS `online_payment_against_subscription` (
  `subscription_od_grp_id` int(11) unsigned NOT NULL, 
  `order_payment_total` decimal(10, 2) unsigned NOT NULL, 
  `user_id` int(11) NOT NULL
) DEFAULT CHARSET = utf8;

INSERT INTO `online_payment_against_subscription` (
  `subscription_od_grp_id`, `order_payment_total`, `user_id` 
) 
VALUES 
  (1643695200, '45.00', 115), 
  (1647692312, '250.00', 115), 
  (1647692222, '30.00', 115);
SELECT 
  sum(y.order_payment_total), 
  sum(s.order_discount) 
FROM 
  subscription s 
  LEFT JOIN(
    SELECT 
      SUM(order_payment_total) as order_payment_total, 
      user_id, 
      subscription_od_grp_id 
    FROM 
      online_payment_against_subscription 
    GROUP BY 
      subscription_od_grp_id
  ) y ON y.subscription_od_grp_id = s.od_grp_id 
WHERE 
  find_in_set(
    s.id, '123994,124255,124703,125788,125789'
  ) 
group by 
  s.user_id
Current Output:
| sum(y.order_payment_total) |sum(s.order_discount)  |
|----------------------------|-----------------------|
|                        530 |                    30 |


Expected Ouput:
| sum(y.order_payment_total) |sum(s.order_discount)  |
|----------------------------|-----------------------|
|                       280  |                    30 |

Sql Fiddle: http://sqlfiddle.com/#!9/5628f5/1

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

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

发布评论

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

评论(2

难如初 2025-01-25 14:44:29

如果我正确理解,问题是由一些重复od_grp_id来自subscription表引起的,因此您可以在加入之前删除重复od_grp_id,所以我们可以在子查询中这样做。

查询1

SELECT 
   SUM(order_payment_total),
   SUM(order_discount)
FROM (
   SELECT od_grp_id,SUM(order_discount) order_discount
   FROM subscription
   WHERE find_in_set(id, '123994,124255,124703,125788,125789') 
   GROUP BY od_grp_id
 ) s 
LEFT JOIN online_payment_against_subscription y ON y.subscription_od_grp_id=s.od_grp_id

结果 强>:

| SUM(order_payment_total) | SUM(order_discount) |
|--------------------------|---------------------|
|                      280 |                  30 |

If I understand correctly, The problem is caused by some duplicate od_grp_id from subscription table, so you might remove the duplicate od_grp_id before JOIN, so we might do that in a subquery.

Query 1:

SELECT 
   SUM(order_payment_total),
   SUM(order_discount)
FROM (
   SELECT od_grp_id,SUM(order_discount) order_discount
   FROM subscription
   WHERE find_in_set(id, '123994,124255,124703,125788,125789') 
   GROUP BY od_grp_id
 ) s 
LEFT JOIN online_payment_against_subscription y ON y.subscription_od_grp_id=s.od_grp_id

Results:

| SUM(order_payment_total) | SUM(order_discount) |
|--------------------------|---------------------|
|                      280 |                  30 |
星軌x 2025-01-25 14:44:29

我认为您收到此错误是因为每个订阅都没有订单付款,即您收到 NULL 值。

您可以尝试使用此方法删除它们 -

SELECT y.order_payment_total
    FROM subscription s
             LEFT JOIN(SELECT SUM(order_payment_total) AS order_payment_total, user_id, subscription_od_grp_id
                       FROM online_payment_against_subscription
                       GROUP BY subscription_od_grp_id) y ON y.subscription_od_grp_id = s.od_grp_id
    WHERE FIND_IN_SET(s.id, '11258,22547,18586')
      AND y.order_payment_total IS NOT NULL;

或者如果需要,您可以将 NULL 值设置为 0 -

SELECT COALESCE(y.order_payment_total, 0) AS order_payment_total
    FROM subscription s
             LEFT JOIN(SELECT SUM(order_payment_total) AS order_payment_total, user_id, subscription_od_grp_id
                       FROM online_payment_against_subscription
                       GROUP BY subscription_od_grp_id) y ON y.subscription_od_grp_id = s.od_grp_id
    WHERE FIND_IN_SET(s.id, '11258,22547,18586');

I think you are getting this error because every subscription doesn't have an order payment that is you are getting NULL values.

You can try to remove them by using this -

SELECT y.order_payment_total
    FROM subscription s
             LEFT JOIN(SELECT SUM(order_payment_total) AS order_payment_total, user_id, subscription_od_grp_id
                       FROM online_payment_against_subscription
                       GROUP BY subscription_od_grp_id) y ON y.subscription_od_grp_id = s.od_grp_id
    WHERE FIND_IN_SET(s.id, '11258,22547,18586')
      AND y.order_payment_total IS NOT NULL;

Or you can make NULL values 0 if you required -

SELECT COALESCE(y.order_payment_total, 0) AS order_payment_total
    FROM subscription s
             LEFT JOIN(SELECT SUM(order_payment_total) AS order_payment_total, user_id, subscription_od_grp_id
                       FROM online_payment_against_subscription
                       GROUP BY subscription_od_grp_id) y ON y.subscription_od_grp_id = s.od_grp_id
    WHERE FIND_IN_SET(s.id, '11258,22547,18586');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文