在mysql中是否可以使用计算的选择值进行排序?例如,我有类似于以下内容的内容:
SELECT members.*, ROUND(formula for finding distance) AS distance
FROM members
ORDER BY distance ASC
但是在执行查询时,我收到一条消息,指出Column not found: 1054 Unknown column 'distance' in 'order clause'
答案 0 :(得分:0)
您不能使用order by
子句的别名。但是,您可以使用相同的表达式(顺便说一下,请注意order by
是两个单词,而不是一个单词):
SELECT members.*, ROUND(formula for finding distance) AS distance
FROM members
ORDER BY ROUND(same formula for finding distance) ASC
答案 1 :(得分:0)
尝试这样的事情:
SELECT members.*, ROUND(formula for finding distance) AS distance
FROM members
ORDER BY ROUND(formula for finding distance) ASC
答案 2 :(得分:0)
在MySQL中,当然可以在ORDER BY
中使用别名。这对于距离计算非常典型:
SELECT members.*, ROUND(formula for finding distance) AS distance
FROM members
ORDER BY distance ASC;
大多数数据库都允许这种构造。
这是一个Rextester,证明它有效。