Reapprove the result of a function in Where

1
Hello, I'm working with MYSQL, I would like to know if I can use the result of a function in the where by calling the alias.

Today I have to repeat the code, in case this GEO function

SELECT *,round(geo(-46.000000,-23.000000,latitude,longitude),3) AS distancia 
FROM view_empresas_pins 
where round(geo(-46.000000,-23.000000,latitude,longitude),3) <= 7

I would like to do something like this:

SELECT *,round(geo(-46.000000,-23.000000,latitude,longitude),3) AS distancia 
FROM view_empresas_pins where distancia <= 7

where the result of the GEO function is used in the where, without having to call again.

    
asked by anonymous 15.01.2018 / 01:36

1 answer

3

You can use the function HAVING of MySQL , so you can work with the alias called in your query , the query would look like this:

SELECT 
     *,
    round(geo(-46.000000,-23.000000,latitude,longitude),3) AS distancia 
FROM view_empresas_pins 
HAVING distancia <= 7
    
15.01.2018 / 17:42