MySQL使用动态变量加入两个表

时间:2013-09-05 15:52:43

标签: php mysql sql google-maps full-text-search

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

最终结果将是一个搜索功能,用于查找离您最近的商店。

搜索:

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

已经停留了相当长的一段时间,似乎无法找到一种很好的方法将两者结合起来,以便找到最近的匹配并对其进行相应的排序。

1 个答案:

答案 0 :(得分: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