gpt4 book ai didi

mysql - 对多个 ORDER BY 列求和

转载 作者:行者123 更新时间:2023-11-29 05:33:31 25 4
gpt4 key购买 nike

我创建了 this问题,女巫现在得到了照顾,但我在同一个问题上遇到了另一个问题:

SELECT
t.type,
SUM( t.external_account )
FROM
u_contracts c,
u_data u,
u_transactions t
WHERE
c.user_id = u.id
AND t.contract_id = c.id
AND t.nulled =0
AND DATE (c.init_date) < DATE (u.dead)
AND u.dead IS NOT NULL
AND t.type != 'repay'
GROUP BY
t.type
ORDER BY
FIELD( t.type, 'initial', 'comission', 'overpay', 'penalty', 'penalty2' );

我得到的结果如下:

+-----------+---------------------------+
| type | SUM( t.external_account ) |
+-----------+---------------------------+
| prolong | 360560.00 |
| reg | 3889.00 |
| reg2 | 301.20 |
| initial | 610628.54 |
| comission | 125623.49 |
| overpay | 6461.57 |
| penalty | 21461.52 |
| penalty2 | 4010.00 |
+-----------+---------------------------+

我需要对类型为 prolong + reg + reg2 的结果求和,并将它们添加到列表的末尾。

现在我完全不知道它们是如何求和的,考虑到它们是 GROUP BY t.type 请求的结果。

最佳答案

试一试:

SELECT 
t.type,
SUM(t.external_account)
FROM
(
SELECT
CASE t.type
WHEN 'prolong' THEN 'prolong + reg + reg2'
WHEN 'reg' THEN 'prolong + reg + reg2'
WHEN 'reg2' THEN 'prolong + reg + reg2'
ELSE t.type
END AS type,
t.external_account
FROM
u_contracts c,
u_data u,
u_transactions t
WHERE
c.user_id = u.id
AND t.contract_id = c.id
AND t.nulled =0
AND DATE (c.init_date) < DATE (u.dead)
AND u.dead IS NOT NULL
AND t.type != 'repay'
) t
GROUP BY
t.type
ORDER BY
FIELD( t.type, 'initial', 'comission', 'overpay', 'penalty', 'penalty2' );

关于mysql - 对多个 ORDER BY 列求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12708023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com