# Problem with WHERE in temporary field

1

I have this query that calculates the distance between 2 points given certain coordinates.

The query runs well, the problem is the WHERE of the query, this is the error:

Error Code: 1054. Unknown column 'dist' in 'where clause'

If I run the query without the WHRE if it throws me the result I want the problem is the WHERE.

What am I doing wrong

``````SELECT p.*,
(
6371 *
(2 *
atan2(
sqrt(
(
sin(
(p.Punto_Latitude__c - 19.4326077) / 2
)
)

*

sin(
(p.Punto_Latitude__c - 19.4326077) / 2
)
)
)

+

(
cos(
)

*

cos(
)
)

*

(
sin(
(p.Punto_Longitude__c - (-99.015725)) / 2
)
)

*

sin(
(p.Punto_Longitude__c - (-99.015725)) / 2
)
)
)
),

sqrt(1 -
(
sin(
(p.Punto_Latitude__c - 19.4326077) / 2
)
)

*

sin(
(p.Punto_Latitude__c - 19.4326077) / 2
)
)
)

+

(
cos(
)

*

cos(
)
)

*

(
sin(
(p.Punto_Longitude__c - (-99.015725)) / 2
)
)

*

sin(
(p.Punto_Longitude__c - (-99.015725)) / 2
)
)
)
)

)
)
) as dist
FROM places as p
WHERE dist > 100
``````

asked by Alberto Siurob 13.10.2017 в 00:41
source

2

The error is correct. The dist field does not exist in the places table. In general, the databases that process the last are the AS, since they do not affect the result of a query.

Therefore, what you can do is something like this:

``````SELECT *
FROM (todo tu select sin el where)
WHERE dist > 100
``````

I already solved it, if it works, it's with `HAVING` instead of `WHERE` .
``````select p.*,