gpt4 book ai didi

mysql - COUNT 后的 SUM 和 Group By MYSQL

转载 作者:行者123 更新时间:2023-11-29 07:30:19 25 4
gpt4 key购买 nike

我想问一下mysql中的SQL。我的堆栈超过 1 小时:(
我有sql:

    SELECT TZL.IsMissed, COUNT(TZL.ChatID) as Amount FROM tblLog TZL group by TZL.IsMissed

结果:

| IsMissed | Amount |
| 0 | 100 |
| 1 | 500 |

我想在“金额”列之后再添加一列,假设新列的名称是 SumAmount
我想要 SumAmount 值为 SUM金额栏的

|错过了 |金额 |金额 |
| 0 | 100 | 100 600 |
| 1 | 500 | 500 600 |

我已经尝试过像下面这样的sql:

SELECT
tbl.*,SUM(tbl.Amount) as SumAmount
FROM
(
SELECT
TZL.IsMissed,
COUNT(TZL.ChatID) AS Amount
FROM
tblLog TZL
GROUP BY
TZL.IsMissed
) tbl
GROUP BY
tbl.IsMissed
WITH ROLLUP

但是使用ROLLUP,结果是添加新的一行,而不是列。任何人都可以教我这个吗?感谢您的回答

最佳答案

有几种方法可以解决这个问题。我将计算 from 子句中的值:

SELECT TZL.IsMissed, COUNT(TZL.ChatID) as Amount, tt.SumAmount
FROM tblLog TZL CROSS JOIN
(SELECT COUNT(*) as SumAmount FROM tblLog) tt
GROUP BY TZL.IsMissed, tt.SumAmount;

关于mysql - COUNT 后的 SUM 和 Group By MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32863311/

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