【问题标题】:SQL Radius Searching in one table connected with anotherSQL Radius Searching在一个与另一个连接的表中
【发布时间】:2015-06-27 07:25:48
【问题描述】:

当我想根据特定的 city_id 从表一中获取所有商店时,你能告诉我如何加入两个表,其中表二中有自己的 geo_lat 和 geo_long 吗?我想获取距离我的商店 25 公里范围内的所有商店。

表一 - 商店: shop_name、店铺描述、town_id

表二 - 城市: city_id、city_name、geo_lat、geo_long

我尝试了很多例子和方法,但没有结果。

我找到了这个距离的代码:

SELECT
  id, (
    3959 * acos (
      cos ( radians(78.3232) )
      * cos( radians( lat ) )
      * cos( radians( lng ) - radians(65.3234) )
      + sin ( radians(78.3232) )
      * sin( radians( lat ) )
    )
  ) AS distance
FROM markers
HAVING distance < 30
ORDER BY distance
LIMIT 0 , 20;

我会非常感谢您的每一个回答。非常感谢。

【问题讨论】:

  • 表一的town_id和表二的city_id一样吗?
  • 是的,表一中的town_id 相当于表二中的city_id。这意味着当我在表 2 中有一个 city_id: 1 和 city_name: prague 的城市,然后我在表 1 中有一个 town_id: 1 的商店 -> 我的商店的这个城市将是布拉格。

标签: php mysql database


【解决方案1】:
    select s.shop_name, s.shop_description, s.town_id, c.city_id, c.city_name, c.geo_lat, c.geo_long from shops s
    right join cities c on c.city_id = s.town_id 
and (
    3959 * acos (
      cos ( radians(78.3232) )
      * cos( radians(c.geo_lat ) )
      * cos( radians( c.geo_long ) - radians(65.3234) )
      + sin ( radians(78.3232) )
      * sin( radians( c.geo_lat ) )
    )
  ) <= 25
    where c.city_name = 'Prague'

【讨论】:

  • 但是我想从第一张桌子获得所有其他商店,这些商店距离 town_id 半径 25 公里...
  • 发布这两个表中的一些记录,这将有助于为您提供更好的答案。我以为你只是想弄清楚如何加入。
  • 我不知道计算是否正确,如果它们正确,这应该会给你你正在寻找的结果。
  • 这里是计算 link 的示例,数字 78.32.32 和 65.3234 必须被替换......接下来的一件事是我认为 where c.city_name = 'Prague 的 where 语句' 也可以替换为 c.city_id = specific_ID_WHICH_I_AM_LOOKING_FOR ... 你能告诉我如何更新坐标吗?非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多