F
F
F(x)2018-03-01 14:14:02
Yii
F(x), 2018-03-01 14:14:02

How to get information within a certain radius?

Good afternoon!
How to get information within a certain radius?
Or how to sort "near me"?
I have a query like this:

SELECT DISTINCT name,
  id, (
    6371 * acos (
      cos ( radians(43.639955) )
      * cos( radians( coords_latitude ) )
      * cos( radians( coords_longitude ) - radians(68.324195) )
      + sin ( radians(43.639955) )
      * sin( radians( coords_latitude ) )
    )
  ) AS distance
FROM areas
#	HAVING distance < 10
ORDER BY distance
  LIMIT 0 ,10

How to implement it in YII2?
How to implement a query so that it returns a list sorted by coordinates (height and width)?
We have a certain point and the list will be sorted depending on the proximity to this point, the closest location point is at the top, the farthest is at the bottom.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bay, 2018-03-01
@kawabanga

On your question, you can of course disassemble the models, nothing complicated. But in such cases, I usually do two subqueries. First I pull out an array with the required calculation. With the second request, I pull out the necessary models.
It is not clear what you mean by what needs to be implemented in Yii2. Run a simple query -
Yii::$app->db->createCommand($sql)->queryAll() - will return you the result of the query.
For reference:
1) use the point data type, mysql has GIS functions for that. And they are easier to use.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question