从单个表中组合两个MySQL SELECT查询

时间:2016-08-22 17:43:45

标签: mysql

我有一个这样的SQL语句:

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

第一部分返回特定距离内的商店,第二部分返回同一表格同一行中给定字段的总和。

两个部分分开工作,但在合并时不会返回结果。

请告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

不要在哪里使用别名

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