duai3681
duai3681
2015-03-04 11:52

使用st_distance在mysql中按距离对用户进行排序

  • php
  • mysql
  • geolocation

I've done a bit of reading on this but there's hardly any information on doing it using points and the st_distance function in mysql. I suppose this makes sense since according to http://bugs.mysql.com/bug.php?id=70494 it wasn't even documented until halfway through last year. Almost every solution I've seen instead uses separate long and lat columns, and then puts them into the haversine formula like this: sorting distance in MySQL PHP This seems like a really messy way of doing it though due to the existance of geometry data types and the spatial functions.

My table so far is:

Username - varchar

Location - Point(1 1) or something like that

I want to choose one user and then display the nearest 10 or so users to them. Is it possible to do this entirely through SQL using points and the st_distance function, or will I have to make some changes and use the haversine formula instead?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐