复杂子计算的MySQL语法

时间:2012-05-06 09:03:45

标签: php mysql select syntax proc

使用以下选择我试图在PHP循环时查看每一行,拉出当前位置半径并使用PROC distance_between计算每个“事件”到当前位置的距离然后填充在转到下一行之前DISTANCEFROMEVENT

我倒下的地方(因为大部分工作都是)正在添加动作

distance_between 52.5960 , -1.9110 ,
GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LATITUDE ,
GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LONGITUDE AS DISTANCEFROMEVENT

我不清楚我如何将其加入SELECT语句的语法,虽然我自己知道它应该有效。

SELECT * FROM Current
    WHERE (GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LATITUDE <53.319589001447
    AND
    GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LATITUDE > 51.872410998553
    AND
    GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LONGITUDE > -2.6349916029658
    AND
    GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LONGITUDE < -1.1870083970342
    AND
    VALIDPERIOD_STARTOFPERIOD < '12-05-06 16:15:31'
    AND
    VALIDPERIOD_ENDOFPERIOD > '12-05-06 16:15:31' ,
    distance_between 52.5960 , -1.9110 , GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LATITUDE , GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LONGITUDE AS DISTANCEFROMEVENT )

我可以获得任何指导。

人族

CREATE FUNCTION distance_between (from_lat DECIMAL(6, 3),
    from_lng DECIMAL(6, 3),
    to_lat DECIMAL(6, 3),
    to_lng DECIMAL(6, 3))
    RETURNS DECIMAL(11, 3)

    RETURN 6371 * 2 * ATAN2(SQRT(POW(SIN(RADIANS(to_lat - from_lat)/2), 2) +
    POW(SIN(RADIANS(to_lng - from_lng)/2), 2) * COS(RADIANS(from_lat)) *
    COS(RADIANS(to_lat))), SQRT(1 - POW(SIN(RADIANS(to_lat - from_lat)/2), 2) +
    POW(SIN(RADIANS(to_lng - from_lng)/2), 2) * COS(RADIANS(from_lat)) * COS(RADIANS(to_lat))));

工作.............

SELECT *, ( 3959 * acos( cos( radians(52.5960) ) * cos( radians( GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LATITUDE ) ) * cos( radians( GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LONGITUDE ) - radians(-1.9110) ) + sin( radians(52.5960) ) * sin( radians( GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LATITUDE ) ) ) ) AS DISTANCEFROMEVENT FROM Current HAVING DISTANCEFROMEVENT < 50 ORDER BY DISTANCEFROMEVENT

1 个答案:

答案 0 :(得分:0)

在您遇到麻烦的部分之前的最后一个搜索字词缺少结束“'”。除非这只是一个错字。如果不是这样,请告诉我,我会再看看

修改

SELECT *, (distance_between 52.5960 , -1.9110 , 
GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LATITUDE , GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LONGITUDE) AS DISTANCEFROMEVENT 
FROM Current WHERE
GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LATITUDE < 53.319589001447
AND GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LATITUDE > 51.872410998553
AND GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LONGITUDE > -2.6349916029658
AND GOFL_LOCCGRP_TPEGILOC_TO_POINTCOORDINATES_LONGITUDE < -1.1870083970342
AND VALIDPERIOD_STARTOFPERIOD < '12-05-06 16:15:31'
AND VALIDPERIOD_ENDOFPERIOD > '12-05-06 16:15:31'

好的,所以你需要在SELECT中调用DISTANCEFROMEVENT字段才能使它出现。