搜索附近的纬度/长查询 - 半径搜索有效,但需要添加另一个字段

时间:2014-10-28 20:02:35

标签: php mysql

我有一个基于已知纬度/经度的当前位置查询。搜索在某个半径内找到匹配。

查询工作正常,但现在我想添加另一篇文章。

假设我在name="Kennedy Middle School"city="Chicago"

中搜索state="IL"

这将产生x英里半径搜索。如果结果不在伊利诺伊州芝加哥市中心的x英里半径范围内,则搜索不会产生任何结果。

我想要包含半径部分,但还要包含基本匹配的查询的另一部分:“Select FROM WHERE [radius query stuff] OR [city="Chicago and state="IL"]

我想.....“在x / y点的5英里半径范围内搜索'牙医',还要给我任何'牙医',这个城市是'芝加哥'

这就是我的查询:

SELECT * 
  FROM (
 SELECT r.name, r.category, r._address, r.city, r.state, r.lat, r.long, domaintable.domain, 
        MATCH(name, category) AGAINST ('kennedy middle school%') as relevance,
        p.radius,
        p.distance_unit
                 * DEGREES(ACOS(COS(RADIANS(p.latpoint))
                 * COS(RADIANS(r.lat))
                 * COS(RADIANS(p.longpoint - r.long))
                 + SIN(RADIANS(p.latpoint))
                 * SIN(RADIANS(r.lat)))) AS distance
  FROM nametable_db AS r
  JOIN (   
        SELECT  28.5383355  AS latpoint,  -81.37923649999999 AS longpoint,
                5 AS radius,      69.0 AS distance_unit
    ) AS p
    INNER JOIN domaintable.domain ON
    r.state = domaintable.domain
  WHERE 
    MATCH(name, category) against ('kennedy middle school*' in boolean mode) AND
    r.lat
     BETWEEN p.latpoint  - (p.radius / p.distance_unit)
         AND p.latpoint  + (p.radius / p.distance_unit)
    AND r.long
     BETWEEN p.longpoint - (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
         AND p.longpoint + (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
 ) AS d
 WHERE distance <= radius
 ORDER BY distance

基本上,我只想将OR WHERE city="Chicago" and state="IL"放入查询中,以便结果也包括在内。

我尝试编辑查询的每个地方,我最终都会讨论或不做任何事情。

任何帮助都非常感激。

感谢。

0 个答案:

没有答案