数据:
+------+-----------+-----------+------------+
| ID | GROUP | LAT | LNG |
+------+-----------+-----------+------------+
| 1 | A | 37.937363 | 1.348025 |
+------+-----------+-----------+------------+
| 4 | A | 60.879875 | 8.789927 |
+------+-----------+-----------+------------+
| 5 | B | 41.637905 | -0.936209 |
+------+-----------+-----------+------------+
| 7 | A | 40.382008 | -3.722799 |
+------+-----------+-----------+------------+
| 8 | B | 47.023994 | 1.808898 |
+------+-----------+-----------+------------+
| 10 | A | 13.369470 | 105.709335 |
+------+-----------+-----------+------------+
| 11 | B | 59.290794 | 14.978157 |
+------+-----------+-----------+------------+
| 13 | A | 16.271467 | 100.917656 |
+------+-----------+-----------+------------+
| 14 | B | 50.878986 | 3.839009 |
+------+-----------+-----------+------------+
| 16 | B | 52.367733 | -1.645905 |
+------+-----------+-----------+------------+
我真的有几千行,这只是一个例子。
通过这个查询,我得到了从我所在的位置到按组和距离排序的每个点的距离:
SELECT *
FROM (
SELECT
*,
IF(lat > 0 OR lng > 0, (3959 * acos(cos(radians(39.571370)) * cos(radians(lat)) * cos(radians(lng) - radians(2.651773)) + sin(radians(39.571370)) * sin(radians(lat)))* 1.609344), 0) AS distance
FROM points
) AS t
ORDER BY t.group, distance ASC
结果:
+------+-----------+-----------+------------+--------------------+
| ID | GROUP | LAT | LNG | DISTANCE |
+------+-----------+-----------+------------+--------------------+
| 1 | A | 37.937363 | 1.348025 | 214.00082160996624 |
+------+-----------+-----------+------------+--------------------+
| 7 | A | 40.382008 | -3.722799 | 550.5073098103634 |
+------+-----------+-----------+------------+--------------------+
| 4 | A | 60.879875 | 8.789927 | 2406.968707155803 |
+------+-----------+-----------+------------+--------------------+
| 13 | A | 16.271467 | 100.917656 | 9548.32105549451 |
+------+-----------+-----------+------------+--------------------+
| 10 | A | 13.369470 | 105.709335 | 10149.183567016042 |
+------+-----------+-----------+------------+--------------------+
| 5 | B | 41.637905 | -0.936209 | 380.1650567381738 |
+------+-----------+-----------+------------+--------------------+
| 8 | B | 47.023994 | 1.808898 | 831.5334259849861 |
+------+-----------+-----------+------------+--------------------+
| 14 | B | 50.878986 | 3.839009 | 1260.8153715832098 |
+------+-----------+-----------+------------+--------------------+
| 16 | B | 52.367733 | -1.645905 | 1460.560861854739 |
+------+-----------+-----------+------------+--------------------+
| 11 | B | 59.290794 | 14.978157 | 2358.22190793295 |
+------+-----------+-----------+------------+--------------------+
期望的结果:
+------+-----------+-----------+------------+--------------------+
| ID | GROUP | LAT | LNG | DISTANCE |
+------+-----------+-----------+------------+--------------------+
| 1 | A | 37.937363 | 1.348025 | 214.00082160996624 |
+------+-----------+-----------+------------+--------------------+
| 7 | A | 40.382008 | -3.722799 | 550.5073098103634 |
+------+-----------+-----------+------------+--------------------+
| 5 | B | 41.637905 | -0.936209 | 380.1650567381738 |
+------+-----------+-----------+------------+--------------------+
| 8 | B | 47.023994 | 1.808898 | 831.5334259849861 |
+------+-----------+-----------+------------+--------------------+
如何获得按距离排序的每组的n条记录?