我有两个函数,一个用于搜索(全文搜索),返回最佳匹配,另一个用于从当前纬度/经度查找最近的位置。
最终结果将是一个搜索功能,可以找到离您最近的商店。
搜索:
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
卡住了很长一段时间,似乎无法找到将它们结合起来的好方法,因此它会找到最接近的匹配项并相应地对其进行排序。