对于我页面上的结果,我需要按距离排序结果。该页面获取用户的当前位置,并检索该位置最接近的30个结果。
在距离的顺序旁边,我还想按价格为每个venue
订购结果。
问题在于价格订单是否有效,而不是距离订单。我无法弄清楚我的函数是如何排序结果的,因为它没有结构(有时最接近的结果是在最后一次放置,有时它在中间,有时在开头,它真的很奇怪)。
我的SQL语句如下:
$lat = '48.213688';
$lng = '16.408229';
$sql = "SELECT *, (3959 * acos(cos(radians('".$lat."')) * cos(radians(venue_address_lat)) * cos(radians(venue_address_lng) - radians('".$lng."')) + sin(radians('".$lat."')) * sin(radians(venue_address_lat)))) AS distance
FROM car_offer JOIN venues_infos ON car_offer.venues_infos_id = venues_infos.venue_id WHERE (3959 * acos(cos(radians('".$lat."')) * cos(radians(venue_address_lat)) * cos(radians(venue_address_lng) - radians('".$lng."')) + sin(radians('".$lat."')) * sin(radians(venue_address_lat)))) < 1.5
AND car_offer.offer_date = CURDATE()
AND venue_address_postcode = 1020
OR car_offer.offer_date = '0000-00-00'
ORDER BY distance, offer_price_small LIMIT 0,30";
数据库中的lat, lng
来自Google地图,因此这应该是准确的。
答案 0 :(得分:0)
我认为你最好在这种情况下使用外部查询,然后在外部查询中执行order by
,如下所示。 (或)您可以在distance
中使用相同的order by
计算表达式。请注意我已将distance < 1.5
从内部查询移到外部查询。此外,我怀疑您是否可以在distance
子句中使用order by
,就像您使用的方式一样(在同一查询级别中,除非它来自派生表表达式),因为distance
是列别名。
SELECT * FROM
(
SELECT *, (3959 * acos(cos(radians('".$lat."')) * cos(radians(venue_address_lat)) * cos(radians(venue_address_lng) - radians('".$lng."')) + sin(radians('".$lat."')) * sin(radians(venue_address_lat)))) AS distance
FROM car_offer
JOIN venues_infos
ON car_offer.venues_infos_id = venues_infos.venue_id
WHERE car_offer.offer_date = CURDATE()
AND venue_address_postcode = 1020
OR car_offer.offer_date = '0000-00-00'
) tab
WHERE distance < 1.5
ORDER BY distance, offer_price_small
LIMIT 0,30