2017-10-10 1 views
1

mysql에서 계산 된 선택 값을 사용하여 주문할 수 있습니까? 예를 들어, 나는 다음과 비슷한 있습니다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

MySQL 또는 MS SQL Server? 앨리어스와 ORDER BY에서 "거리"의 철자가 일치하지 않으면 MySQL에서 작동합니다. – Uueerdo

답변

1

당신은 order by 절 별칭을 사용할 수 없습니다라는 메시지와 함께 충족하고있다. 물론 별칭 MySQL의에서

SELECT members.*, ROUND(formula for finding distance) AS distance 
FROM members 
ORDER BY ROUND(formula for finding distance) ASC 
+1

MySQL은 SELECT 필드의 별칭을 WHERE 절 (및 다른 SELECT 필드의 식)과 거의 모든 곳에서 사용할 수 있습니다. – Uueerdo

0

이런 식으로 뭔가를 시도 : 당신은, 그러나, 단지 (order by이 두 단어가 있는지를하지 유의 BTW) 같은 표현을 사용할 수 있습니다 ORDER BY에서 사용할 수 있습니다. 이것은 거리 계산에 매우 일반적입니다.

SELECT members.*, ROUND(formula for finding distance) AS distance 
FROM members 
ORDER BY distance ASC; 

대부분의 데이터베이스는이 구조를 허용합니다.

여기에 Rextester이 있습니다.

0

:

SELECT members.*, ROUND(formula for finding distance) AS distance 
FROM  members 
ORDER BY ROUND(same formula for finding distance) ASC 
관련 문제