【发布时间】:2021-04-09 08:52:41
【问题描述】:
我对 wordpress 有点困惑,我有以下查询来计算帖子之间的距离:
public function distance($latitude, $longitude){
global $wpdb;
$sql = "SELECT l.post_id,( 6371 * acos( cos( radians($latitude) ) * cos( radians( l.latitude ) )
* cos( radians(l.longitude) - radians($longitude)) + sin(radians($latitude))
* sin( radians(l.latitude)))) AS distance
FROM localisation as l
HAVING distance < 50
ORDER BY distance ASC";
$output = $wpdb->get_results($sql, ARRAY_A);
dump($output) // <--- Here i test the result
}
输出显示从最近到最远(大约 50 公里)的帖子,除了当我复制/粘贴相同的查询时,我没有相同的结果(产品从最近到最远的顺序排列,但不是相同的顺序)
此查询的输出:
post_id. | distance.
1245 0.25029229952593496
8547 0.25029229952593496
78451 0.25029229952593496
45874 0.4428014744990856
但是来自 phpmyadmin 的输出:
post_id. | distance.
78451 0.25029229952593496
8547 0.25029229952593496
1245 0.25029229952593496
45874 0.4428014744990856
顺序不一样,连排序都好
你有什么想法吗?为什么我得到不同的订单?
谢谢
【问题讨论】:
-
由于其中三个记录显示精确相同的距离,您需要添加一个二级订单列以确保它们以相同的顺序返回。没有它,您就不会告诉 MySQL 如何对相同的值进行排序,因此您永远无法假设它们的顺序。