在WHERE子句中重用多个变量

时间:2012-03-21 01:13:07

标签: php mysql where-clause code-reuse

我有一个map字段,其中包含以分号分隔的纬度和经度。我需要做的是将两者分开,然后从另一个给定的纬度和经度进行计算以计算差异。

注意,请忽略...我如何拆分地图字符串字段,b.id = a.id,以及我在lat和long上进行的计算。它们在这里并不重要。

WHERE 

/* Here's where I get the values for lat and long from the map field */
(
    SELECT 
    /* lat from map field */ as lat, 
    /* long from map field */ as long
FROM some_table AS b 
    WHERE b.id = a.id AND b.key = 'map'
)

/* Here's where I use them to do some calculations... */
lat <= (" . ($lat + $radius / 40075) . ")
AND lat >= (" . ($lat - $radius / 40075) . ")
AND long <= (" . ($long + $radius / 40075) . ")
AND long >= (" . ($long - $radius / 40075) . ")

我知道这不起作用,但我想知道如何操作,所以每次使用latlong时我都不会重新选择地图值。< / p>

1 个答案:

答案 0 :(得分:0)

我无法对此进行测试,因此希望有人能解决任何语法错误。

SELECT SUBST(map, 1, STRPOS(map, ",") - 1) AS lat, SUBSTR(map, STRPOS(map, ",")) AS long
FROM some_table
WHERE
    lat BETWEEN ($lat - $radius / 40075) AND ($lat + $radius / 40075)
AND 
    long BETWEEN ($long - $radius / 40075) AND ($long + $radius / 40075)