您可以在MySQL中将一个别名除以另一个别名吗? [英] Can you divide one alias by another in MySQL?

查看:75
本文介绍了您可以在MySQL中将一个别名除以另一个别名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a multi-table query, similar to this (simplified version)

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错误,如下所示:

The problem is in the ORDER BY clause. This causes a MySQL error to show, which is as follows:

不支持参考"sum_ rev_ rating"(参考组功能)

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

推荐答案

您无法使用别名进行计算.一种方法是简单地以此创建另一个别名和顺序.

You're not able to do calculations with aliases. One way of doing this would be to simply create another alias and order by that.

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中将一个别名除以另一个别名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆