I have a ads table where I have featured and non-featured ads with latitude and longitude values, when user do searching of ads distance wise from a certain location, I want to show the featured ads at the top which are under 500 km from the selected location, and all other ads distance wise to unlimited distance followed by featured listings.
I am able to show featured listings first to unlimited distance and then other remaining ads using distance function and order by featured clause. But how to make featured listings showing under 500km first and then remaining ads to unlimited distance.
do I need to use union, is there any other solution?
source location's
latitude = -37.814563
longitude = 144.970267
SELECT da.*,( 6371 * acos( cos(radians(144.970267)) * cos(radians(da.loc_lng)) *
cos(radians(-37.814563) - radians(da.loc_lat)) + sin(radians(144.970267)) *
sin(radians(da.loc_lng)) )) as distanceSort FROM directory_ads da WHERE da.approved = 1
AND da.deleted = 0 ORDER BY da.featured DESC, distanceSort ASC
You can do this with the following order by
clause with a condition:
order by (case when da.featured > 0 and DistanceSort < 500
then distance_sort else 500
end),
DistanceSort