首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >您能在MySQL中将一个别名除以另一个别名吗?

问您能在MySQL中将一个别名除以另一个别名吗?
EN

Stack Overflow用户
提问于 2008-10-10 05:40:38
回答 2查看 12.3K关注 0票数 10

我有一个多表查询,类似于此(简化版本)。

代码语言:javascript
复制
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错误,如下所示:

不支持

引用“sum_ rev_评级”(引用组函数)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2008-10-10 06:36:23

你不能用化名来计算。这样做的一种方法是简单地创建另一个别名并根据别名进行排序。

代码语言:javascript
复制
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
票数 14
EN

Stack Overflow用户

发布于 2008-10-10 06:23:48

我的mysql生锈了,你可以试试

代码语言:javascript
复制
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 rev_ratio
FROM table1
    LEFT JOIN table2ON table1.dom_id = table2.rev_domain_from 
WHERE dom_lastreview != 0 
AND rev_status = 1 
GROUP BY dom_url 
ORDER BY rev_Ratio DESC

或

代码语言:javascript
复制
SELECT * from (
    SELECT columns, count(table2.rev_id) As rev_count, 
        sum(table2.rev_rating) As sum_rev_rating 
    FROM table1
        LEFT JOIN table2ON table1.dom_id = table2.rev_domain_from 
    WHERE dom_lastreview != 0 
    AND rev_status = 1 
    GROUP BY dom_url 
) X
ORDER BY X.sum_rev_rating/X.rev_count DESC

或

代码语言:javascript
复制
SELECT * from (
    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 rev_ratio
    FROM table1
        LEFT JOIN table2ON table1.dom_id = table2.rev_domain_from 
    WHERE dom_lastreview != 0 
    AND rev_status = 1 
    GROUP BY dom_url 
) X
ORDER BY rev_Ratio DESC
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/190251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档