0

我有两个函数,一个用于搜索(全文搜索),返回最佳匹配,另一个用于从当前纬度/经度查找最近的位置。

最终结果将是一个搜索功能,可以找到离您最近的商店。

搜索:

SELECT *,MATCH(name) AGAINST('*McDonalds*' IN BOOLEAN MODE) AS `score_name_0`, MATCH(`city`)   
AGAINST('McDonalds*' IN BOOLEAN MODE) AS `score_city_0` FROM `stores` 

WHERE MATCH(name) AGAINST('*McDonalds*' IN BOOLEAN MODE) OR 
MATCH (`city`) AGAINST('McDonalds*' IN BOOLEAN MODE) 
ORDER BY  (`score_name_0` + `score_city_0`) DESC

寻找最近的:

SELECT *, (3959 * acos(cos(radians('.$lat.')) * cos(radians(lat)) * cos(radians(lng) - 
radians('.$long.')) + sin(radians('.$lat.')) * sin(radians(lat)))) AS distance 

FROM pinpoints 
INNER JOIN stores WHERE stores.id=pinpoints.store  HAVING distance < 25 ORDER BY distance

我的尝试:

SELECT *,MATCH(name) AGAINST('*McDonalds*' IN BOOLEAN MODE) AS `score_name_0`, 
MATCH(`city`) AGAINST('McDonalds*' IN BOOLEAN MODE) AS `score_city_0` 

FROM `stores` 
JOIN pinpoints ON (3959 * acos(cos( radians(".$lat.")) * cos(radians(pinpoints.lat)) * cos(radians(pinpoints.lng) - 
radians(".$long.")) + sin( radians(".$lat.")) * sin(radians(pinpoints.lat)))) AS distance 

WHERE stores.id=pinpoints.id 
AND MATCH(name) AGAINST('*McDonalds*' IN BOOLEAN MODE) OR 
MATCH (`city`) AGAINST('McDonalds*' IN BOOLEAN MODE) 
HAVING distance < 25 ORDER BY distance, (`score_namn_0` + `score_stad_0`) DESC

卡住了很长一段时间,似乎无法找到将它们结合起来的好方法,因此它会找到最接近的匹配项并相应地对其进行排序。

4

1 回答 1

0

你到处都有东西,我没有测试过这个,但它应该可以工作......

SELECT *
    FROM (SELECT *, MATCH(name) AGAINST('*McDonalds*' IN BOOLEAN MODE) AS `score_name_0`, 
            MATCH(`city`) AGAINST('McDonalds*' IN BOOLEAN MODE) AS `score_city_0`,
        (3959 * acos(cos( radians(".$lat.")) * cos(radians(pinpoints.lat)) * cos(radians(pinpoints.lng) - radians(".$long.")) + sin( radians(".$lat.")) * sin(radians(pinpoints.lat)))) AS distance
        FROM `stores` 
            JOIN pinpoints ON stores.id=pinpoints.id
        WHERE stores.id=pinpoints.id 
            AND (MATCH(name) AGAINST('*McDonalds*' IN BOOLEAN MODE) OR 
            MATCH (`city`) AGAINST('McDonalds*' IN BOOLEAN MODE))
) AS tmp_tbl
WHERE distance < 25 
ORDER BY distance ASC, (`score_namn_0` + `score_stad_0`) DESC
于 2013-09-05T16:01:42.390 回答