gpt4 book ai didi

mysql - 如何计算别名列上的聚合函数 SUM?

转载 作者:可可西里 更新时间:2023-11-01 06:39:31 27 4
gpt4 key购买 nike

如何在别名列上计算聚合函数 SUM?

SELECT a.question_id, 
a.level,
Count(a.question_id) AS rank,
Sum(rank) AS total
FROM logs AS a,
question AS b
WHERE a.question_id = b.q_id
AND a.level = '2'
GROUP BY a.question_id
ORDER BY rank DESC

最佳答案

只需用 (SELECT alias) 包装您重复使用的别名:

SELECT a.question_id, 
a.level,
COUNT(a.question_id) AS rank,
SUM(SELECT(rank)) AS total
FROM logs AS a,
question AS b
WHERE a.question_id = b.q_id
AND a.level = '2'
GROUP BY a.question_id
ORDER BY rank DESC

关于mysql - 如何计算别名列上的聚合函数 SUM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14217944/

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