我有两个MySQL表:
如果不存在,则创建表 "wp_zipcode" ( 'zip' int(5) 不空AUTO_INCREMENT, 'lat' float(10,6) 不为空, 'LNG'浮子(10,6)不为空, 主键("压缩"))
和
如果不存在,则创建表"wp_events"( 'EventID' int(11) 不为空, '事件' varchar(256) 不为空, 'Zip' int(11) 不为空,
不幸的是,我无法更改wp_events(它随时被新数据覆盖),并且我正在对离用户最近的事件进行"距离"搜索。我正在使用谷歌的例子在这里找到
( 3959 * acos( cos( radians(34.070358) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-118.349258) ) + sin( radians(34.070358) ) * sin( radians( lat ) ) ) ) AS distance FROM wp_events HAVING distance < 25 ORDER BY distance LIMIT 0 , 20
我的问题是这样的:为了使算法起作用,我需要有邮政编码的 lng 和纬度,但由于我在wp_events中搜索事件,有没有办法从wp_events获取事件的邮政编码,不知何故(这就是我的问题出现的地方)从 wp_zipcodes 获取该特定邮政编码的相应纬度和 lng 并返回到 SQL 语句以获取适当的所需邮政编码附近的事件。
提前致谢
SELECT wp_events.*,
wp_zipcode.lat,
wp_zipcode.lng,
( 3959 * acos( cos( radians(34.070358) ) * cos( radians( wp_zipcode.lat ) ) * cos( radians( wp_zipcode.lng ) - radians(-118.349258) ) + sin( radians(34.070358) ) * sin( radians( wp_zipcode.lat ) ) ) ) AS distance
FROM wp_events
LEFT JOIN wp_zipcode
ON wp_zipcode.zip = wp_events.Zip
HAVING distance < 25
ORDER BY distance
LIMIT 0 , 20
您可以使用 JOIN
,如下所示:
SELECT wp_events.EventId, wp_events.Event, wp_events.Zip, wp_zipcode.lat, wp_zipcode.lng
FROM wp_events
JOIN wp_zipcode ON wp_events.Zip = wp_zipcode.zip