在距离计算中使用别名

时间:2016-05-31 02:09:26

标签: mysql

我有两张桌子:候选人和candidate_locationcandidate_location表根据其type字段从其他3个表中获取地图坐标。结果是别名latlng,它们需要在IF语句之后的查询部分中用于计算距离。

我收到错误,它在距离计算部分找不到“lat”或“lng”列,我猜是因为它们是别名。我怎样才能对其进行重组,以便在计算中识别它们?

以下是查询:

SELECT candidate.CandID,
    IF (candidate_location.type = 1, p.latitude, IF (candidate_location.type = 2, a.latitude, pr.latitude)) AS lat,
    IF (candidate_location.type = 1, p.longitude, IF (candidate_location.type = 2, a.longitude, pr.longitude)) AS lng,
    IF (candidate_location.type = 2, a.standard_deviation,null) as standard_deviation, 
   ( 3959 * acos( cos( radians(51.41019) ) * cos( radians(lat ) ) 
   * cos( radians(lng) - radians(0.07222)) + sin(radians(51.41019)) 
   * sin( radians(lat)))) AS distance 

    FROM candidate_location

    LEFT JOIN geo_postcodes AS p ON (candidate_location.type = 1 AND candidate_location.postal_id  = p.id) 
    LEFT JOIN geo_area_averages AS a ON (candidate_location.type = 2 AND candidate_location.postal_id  = a.id)
    LEFT JOIN geo_probability AS pr ON (candidate_location.type = 3 AND candidate_location.postal_id  = pr.id) 
LEFT JOIN candidate ON candidate_location.candid=candidate.CandID 

2 个答案:

答案 0 :(得分:3)

正如Ashwin所提到的,您不能将结果列名直接用作计算中的字段。您需要重新进行重新设置...除非您使用MySql变量。它们像内联编程一样工作。设置变量值,然后使用它,但您仍然可以将该值保留为相关列名称。

SELECT 
      candidate.CandID,
      @varLat := COALESCE( p.latitude, a.latitude, pr.latitude) AS lat,
      @varLong := COALESCE( p.longitude, a.longitude, pr.longitude) AS lng,
      COALESCE( a.standard_deviation, null) as standard_deviation, 
      ( 3959 * acos( cos( radians(51.41019) ) * cos( radians(@varLat ) ) 
         * cos( radians(@varLat) - radians(0.07222)) + sin(radians(51.41019)) 
         * sin( radians(@varLong)))) AS distance
   FROM 
      ( select @varLat := 0.00, @varLong := 0.00) sqlvars,
      candidate_location CanLoc
         LEFT JOIN geo_postcodes AS p 
            ON (CanLoc.type = 1 AND CanLoc.postal_id  = p.id) 
         LEFT JOIN geo_area_averages AS a 
            ON (CanLoc.type = 2 AND CanLoc.postal_id  = a.id)
         LEFT JOIN geo_probability AS pr 
            ON (CanLoc.type = 3 AND CanLoc.postal_id  = pr.id) 
         LEFT JOIN candidate 
            ON CanLoc.candid=candidate.CandID 

另外,我使用的是COALESCE()而不是嵌套的IF()块。返回第一个非null值,因此,由于您的连接基于类型1,2或3,因此只有该行具有要限定的值。

答案 1 :(得分:2)

您在使用它们的同一查询中定义别名。尝试在主查询之外使用distance子句,以便您可以使用子查询中的lat和lng,如下所示:

Select tTable.*, ( 3959 * acos( cos( radians(51.41019) ) * cos( radians(lat ) ) 
* cos( radians(lng) - radians(0.07222)) + sin(radians(51.41019)) 
* sin( radians(lat)))) AS distance 
from (
SELECT candidate.CandID,
IF (candidate_location.type = 1, p.latitude, IF (candidate_location.type = 2, a.latitude, pr.latitude)) AS lat,
IF (candidate_location.type = 1, p.longitude, IF (candidate_location.type = 2, a.longitude, pr.longitude)) AS lng
IF (candidate_location.type = 2, a.standard_deviation,null) as standard_deviation  

FROM candidate_location

LEFT JOIN geo_postcodes AS p ON (candidate_location.type = 1 AND candidate_location.postal_id  = p.id) 
LEFT JOIN geo_area_averages AS a ON (candidate_location.type = 2 AND candidate_location.postal_id  = a.id)
LEFT JOIN geo_probability AS pr ON (candidate_location.type = 3 AND candidate_location.postal_id  = pr.id) 
LEFT JOIN candidate ON candidate_location.candid=candidate.CandID ) tTable