user3314105
user3314105

Reputation: 15

Use an alias into Sql request

I want to use an alias in the following sql request.

I can't find a workable way to do this.

Where is my error please ?

Thanks.

SELECT * , get_distance_metres('44.12306832854452',  '-77.34566731195382', Latitude, Longitude)
AS proximite_r
FROM myDB
WHERE  `Status` =  '1'
AND  `proximite_r` <  '1000'
ORDER BY proximite_r ASC

This line

AND  `proximite_r` <  '1000'

Returns the following error

#1054 - Unknown column 'proximite_r' in 'where clause'

Upvotes: 1

Views: 52

Answers (1)

Toothbrush
Toothbrush

Reputation: 2141

The WHERE expression is processed before the SELECT expression; therefore, you can not use an alias in the WHERE expression.

However, with MySQL, you could use HAVING (see https://dev.mysql.com/doc/refman/5.0/en/select.html):

SELECT * , get_distance_metres('44.12306832854452',  '-77.34566731195382', Latitude, Longitude)
AS proximite_r
FROM myDB
WHERE  `Status` =  '1'
HAVING `proximite_r` <  '1000'
ORDER BY proximite_r ASC

Also, proiximate_r should be an int or bigint and you should use < 1000, instead of < '1000'.

Upvotes: 1

Related Questions