Here is my SQLFiddle and here is my procedure
I want to rewrite this procedure to simple operation.
It should take the input as Lat, Lng and return the nearest driverid, lat, long (Order by desc and taking the first record) with respect to the given Lat and Long. It contains someother tables which i don't need.
(The problem is i can't able to find which field is required and which is not)
DELIMITER //
CREATE PROCEDURE `driver_latlong`(IN `lat` DECIMAL(20,9), IN `lng` DECIMAL(20,9))
BEGIN
SET @sql = CONCAT('SELECT sd.DriverId, sd.Lat, sd.Long, sd1.RadiusOfService, @dist:= ( 6371 * acos( cos( radians(',lat,') ) * cos( radians( sd.Lat ) ) * cos( radians( sd.Long ) - radians(',lng,') ) + sin( radians(',lat,') ) * sin( radians( sd.Lat ) ) ) ) AS distance
, Case When @dist <= 1 Then sd1.DeliveryFeeOne When @dist<= 2 Then sd1.DeliveryFeeTwo When @dist<= 3 Then sd1.DeliveryFeeThree Else sd1.DeliveryFeeMore End as deliveryfee
, Case When @dist <= 1 Then sd1.DeliveryTimeOne When @dist<= 2 Then sd1.DeliveryTimeTwo When @dist<= 3 Then sd1.DeliveryTimeThree Else sd1.DeliveryTimeMore End as deliverytime
, sd.ShopName, sd.Address, sd.LogoFile FROM driver_latlong sd LEFT JOIN WHERE sd.Status = \'1\' and sd.Lat != 0 and sd.Long != 0 AND ( 6371 * acos( cos( radians(',lat,') ) * cos( radians( sd.Lat ) ) * cos( radians( sd.Long ) - radians(',lng,') ) + sin( radians(',lat,') ) * sin( radians( sd.Lat ) ) ) ) <= sd1.RadiusOfService ORDER BY distance ASC;');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
Note : Actually i got this procedure from previous project so other tables are not required only table we refer is driver_latlong