MySQL Union 在两个表上根据 GIS 点查找最接近的结果
我正在尝试查找海滩的潮汐信息。我有两个带有经纬度值的海滩表,在提取海滩结果时,我还想查询潮汐表并根据潮汐中最近的纬度经度确定该海滩的高潮和低潮桌子。 我有两个表,其结构如下:
这是我的表结构:
table 'beaches'
pri_id location lat lng
1 seal beach 38.344 -122.877
2 newport 37.877 -121.988
3 rocks 34.987 -122.344
table 'tides'
pri_id lat lng low_time high_time
1 38.565 -123.454 05:00 13:00
2 42.343 -121.234 06:00 14:00
3 42.453 -122.433 05:30 13:30
我当然不想在这里听起来很懒,只是没有找到很多有关最佳方法的信息。 本质上我需要获得 beachs.lat、beachs.lng值,然后查询潮汐表,找到最近的tides.lat和tides.lng,然后根据最近的位置返回tides.high_time和tides.low_time。
我正在使用这个联合,但没有返回任何结果。
(SELECT tide_date,tide_time,lat,lng FROM (SELECT * FROM tides ORDER BY lat ASC, lng ASC) a WHERE a.lat<=200 AND a.lng<=200 LIMIT 0,5)
UNION
(SELECT location,lat, lng FROM (SELECT * FROM beaches ORDER BY lat ASC, lng ASC) b
WHERE b.lat>200 AND b.lng>200
LIMIT 0,5)
我确信有一种更精确的方法可以做到这一点,只是我无法理解这一点。
I am attempting to locate tide information for beaches. I have a two table of beaches with latitude longitude values, and when pulling up the results for the beaches, I also want to query the tides table and ascertain the high and low tide for this beach, based on the nearest latitude longitude in the tides table.
I have two tables which are structured below:
Here is my table structure:
table 'beaches'
pri_id location lat lng
1 seal beach 38.344 -122.877
2 newport 37.877 -121.988
3 rocks 34.987 -122.344
table 'tides'
pri_id lat lng low_time high_time
1 38.565 -123.454 05:00 13:00
2 42.343 -121.234 06:00 14:00
3 42.453 -122.433 05:30 13:30
I certainly dont mean to sound lazy here, just not finding a lot of information about the best way to do this.
Essentially I need to gain
beaches.lat, beaches.lng values, then query the tides table, find the nearest tides.lat and tides.lng and then return the tides.high_time and tides.low_time based on the nearest location.
I am using this union, but getting no results returned.
(SELECT tide_date,tide_time,lat,lng FROM (SELECT * FROM tides ORDER BY lat ASC, lng ASC) a WHERE a.lat<=200 AND a.lng<=200 LIMIT 0,5)
UNION
(SELECT location,lat, lng FROM (SELECT * FROM beaches ORDER BY lat ASC, lng ASC) b
WHERE b.lat>200 AND b.lng>200
LIMIT 0,5)
I am sure there is a more precise method to do this, just cant get my head wrapped around this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将查询放在 http://gis.stackexchange.com 上,读者可能会处理此类数据,从而获得更好的帮助一直。
您想要计算半正矢公式,有多种方法可以实现此目的。
这是我发现的一个页面,其中包含一些示例半正矢计算。
http://www.movable-type.co.uk/scripts/latlong。 html
这里有一个链接,其中包含 SQL 中的一些三角函数语法。我没有使用过MySql,所以我不知道是否支持所有三角函数。
http://www.coderecipes.net/sql-trigonometric-functions.aspx
当然,关于什么更好的讨论还有很多... https://gis. stackexchange.com/questions/4906/why-is-law-of-cosines-more-preferable-than-haversine-when-calculate-distance-be
很抱歉我不能只是敲出你想要的 SQL,但我希望这会有所帮助。
You might get better help if you put your query on http://gis.stackexchange.com, where the readers deal with this kind of data all the time.
You're looking to calculate a haversine formula, and there are several ways to do this.
Here is a page I found with some sample haversine calculations.
http://www.movable-type.co.uk/scripts/latlong.html
Here's a link with some trig function syntax in SQL. I've not used MySql, so I don't know if all trig functions are supported.
http://www.coderecipes.net/sql-trigonometric-functions.aspx
Of course, there's plenty of discussion as to what's better... https://gis.stackexchange.com/questions/4906/why-is-law-of-cosines-more-preferable-than-haversine-when-calculating-distance-be
I'm sorry I can't just whack out the SQL you want, but I hope this helps.