Mysql
 sql >> Database >  >> RDS >> Mysql

Come posso usare la funzione nella mia query MySQL?

Ho creato la funzione di seguito in MYSQL

DELIMITER $$    
DROP FUNCTION IF EXISTS `great_circle_distance`$$
    CREATE DEFINER=`root`@`localhost` FUNCTION `great_circle_distance`(
    lat1 DOUBLE(10,6),
    lon1 DOUBLE(10,6),
    lat2 DOUBLE(10,6),
    lon2 DOUBLE(10,6)
    ) RETURNS double(10,2)
        DETERMINISTIC
    BEGIN
                    DECLARE delta_lat DOUBLE(10,6);
                    DECLARE delta_lon DOUBLE(10,6);
                    DECLARE temp1 DOUBLE(10,6);
                    DECLARE EARTH_RADIUS DOUBLE(10,2);
                    DECLARE distance DOUBLE(10,2);
                    SET lat1 = RADIANS(lat1);
                    SET lon1 = RADIANS(lon1);
                    SET lat2 = RADIANS(lat2);
                    SET lon2 = RADIANS(lon2);
                    SET delta_lat = lat2 - lat1;
                    SET delta_lon = lon2 - lon1;

                    SET temp1 = pow(sin(delta_lat/2.0),2) + cos(lat1) * cos(lat2) * pow(sin(delta_lon/2.0),2);
                    SET EARTH_RADIUS = 3956.0;
                    SET distance = EARTH_RADIUS * 2 * atan2(sqrt(temp1),sqrt(1-temp1));
                    RETURN distance;
        END$$

    DELIMITER ;

Usandolo come

Select great_circle_distance(z.lat,z.log, 32.2342,-72.42342) AS Distance from tbl_abc AS z;