gpt4 book ai didi

mysql - 你能在 MySQL 中将一个别名除以另一个吗?

转载 作者:可可西里 更新时间:2023-11-01 06:28:45 26 4
gpt4 key购买 nike

我有一个多表查询,类似这个(简化版)

SELECT columns, count(table2.rev_id) As rev_count, sum(table2.rev_rating) As sum_rev_rating 
FROM table1
LEFT JOIN table2
ON table1.dom_id = table2.rev_domain_from
WHERE dom_lastreview != 0 AND rev_status = 1
GROUP BY dom_url
ORDER BY sum_rev_rating/rev_count DESC

问题出在 ORDER BY 子句中。这会导致显示 MySQL 错误,如下所示:

Reference 'sum_ rev_ rating' not supported (reference to group function)

最佳答案

您无法使用别名进行计算。一种方法是简单地创建另一个别名并按其排序。

SELECT columns, count(table2.rev_id) As rev_count, sum(table2.rev_rating) As sum_rev_rating, sum(table2.rev_rating)/count(table2.rev_id) as avg_rev_rating
FROM table1
LEFT JOIN table2
ON table1.dom_id = table2.rev_domain_from
WHERE dom_lastreview != 0 AND rev_status = 1
GROUP BY dom_url
ORDER BY avg_rev_rating DESC

关于mysql - 你能在 MySQL 中将一个别名除以另一个吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/190251/

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