Alex Betev Alex Betev - 3 months ago 17
MySQL Question

Combine two MySQL SELECT queries from single table

I have an SQL statement like this:

SELECT
(111.045 * DEGREES(ACOS(COS(RADIANS({$lat}))
* COS(RADIANS(lat))
* COS(RADIANS({$lon}) - RADIANS(lon)) + SIN(RADIANS({$lat}))
* SIN(RADIANS(lat))))) AS distance,
(`f1` + `f2` + `f5` + `f6`) AS sum
FROM `shops`
WHERE distance <= 25


where first part returns shops within certain distance and second part returns sum of given fields in the same row of the same table.

Both parts work separately but do not return results when combined.

Please tell me what i do wrong?

Answer

Don't use alias in where

SELECT 
(111.045 * DEGREES(ACOS(COS(RADIANS({$lat})) 
* COS(RADIANS(lat)) 
* COS(RADIANS({$lon}) - RADIANS(lon)) + SIN(RADIANS({$lat})) 
* SIN(RADIANS(lat))))) AS distance, 
(f1 + f2 + f5 + f6) AS sum 
FROM shops 
WHERE   (111.045 * DEGREES(ACOS(COS(RADIANS({$lat})) 
* COS(RADIANS(lat)) 
* COS(RADIANS({$lon}) - RADIANS(lon)) + SIN(RADIANS({$lat})) 
* SIN(RADIANS(lat))))) <= 25