doubeiji2602 2013-11-27 12:37
浏览 52
已采纳

MySQL ORDER BY最近的区域

Basically I have to get the algorithm to show restaurants in the selected zone and in other zones which actually makes sense..

E.g: if my area is in North of some city, I would like to show up restaurants in north, then move to central and then to east and then to west and finally to the extreme opposite i.e., south

In case of east, then central, north, south, finally extreme opposite i.e., west

I have in my db the following

zones table with ids, 1 - North, 2 - East, 3 - West, 4 - South, 5 - Central.

And localities table with locality/area of each city and structure goes as

locality_id | locality_name | zone_id(FK)

I have my model (php/codeigniter)

$this->db->select('menu_item.restaurant_id, menu_item.price, localities.locality_name, restaurant_information.restaurant_name, restaurant_information.restaurant_address, restaurant_information.is_halal, restaurant_information.cuisine, restaurant_information.city, restaurant_information.pincode');
$this->db->from('menu_item');
$this->db->where('menu_item.dish_id', $dish_id);
$this->db->where('menu_item.is_active', 1);
$this->db->where('restaurant_information.is_active', 1);
$this->db->join('restaurant_information', 'menu_item.restaurant_id = restaurant_information.restaurant_id');
$this->db->join('localities', 'restaurant_information.locality_id = localities.locality_id');

Its okay if I have too many joins or whatever.. but definitely not with lat/long or google geo..

Please help me out.. I tried order_by_field.. Its okay and it works but I am not able to give it dynamically..

Is there any solution or am I headed in the wrong direction..? Correct me if I got the structure wrong..!

And I am also ready if the order by part could be done on the result object where I can fetch the result and sort it based on the location.. but I prefer MySQL to do the job. Thanks in advance

  • 写回答

1条回答 默认 最新

  • douzhong6480 2013-11-27 12:48
    关注

    Basically, if I understand well you want to sort by the closest zone.

    So maybe you should translate your ids 1, 2, 3, 4, 5 into coordinates like:

    • 1 North: x:0, y:1
    • 2 East: x:1, y:0
    • 3 West: x:-1, y:0
    • 4 South: x:0, y:-1
    • 5 Center: x:0, y:0

    Then sort them by distance calculated.


    Example: You are located in West and you have restaurants located in West, North, South and Central

    If you have the coordinates you can calculate the distance then sort your results:

    • West restaurant distance 0
    • North restaurant distance 1.2xxx (something like that)
    • South restaurant distance 1.2xxx (something like that)
    • Central restaurant distance 1

    So, assuming you have implemented the functions GET_DISTANCE() & GET_COORD() you would have

    SELECT
      menu_item.restaurant_id
    , menu_item.price
    , localities.locality_name
    , restaurant_information.restaurant_name
    , restaurant_information.restaurant_address
    , restaurant_information.is_halal
    , restaurant_information.cuisine
    , restaurant_information.city
    , restaurant_information.pincode
    FROM menu_item
    JOIN restaurant_information
      ON (menu_item.restaurant_id = restaurant_information.restaurant_id)
    JOIN localities
      ON (restaurant_information.locality_id = localities.locality_id)
    WHERE TRUE
      AND menu_item.dish_id = $dish_id
      AND menu_item.is_active = 1
      AND restaurant_information.is_active = 1
    ORDER BY
      GET_DISTANCE(
        GET_COORD($my_position)
      , GET_COORD(restaurant_information.locality_id)
      )
    ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行