我对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顺序是不一样的,即使排序也是好的。
你有主意吗?为什么我有不同的顺序?
谢谢
发布于 2021-04-09 09:26:38
我认为这是因为你应该先按“距离”订购,然后再按“id”订购。默认情况下,Php我的管理员由id 'DESC‘命令,但wp通过id 'ASC’命令它,您应该在wp查询中尝试如下所示
ORDER BY distance ASC, ID ASC或
ORDER BY distance ASC, ID DESC发布于 2021-04-09 09:27:11
命令两列。
如果您希望得到第一个结果,请按post_id ASC命令,距离ASC。
https://stackoverflow.com/questions/67018001
复制相似问题