salix/db/changes/10081-agency/00-address_getGeo.sql

28 lines
566 B
SQL

DROP FUNCTION IF EXISTS `vn`.`address_getGeo`;
DELIMITER $$
CREATE DEFINER=`root`@`%` FUNCTION `vn`.`address_getGeo` (vSelf INT)
RETURNS INT
DETERMINISTIC
BEGIN
/**
* Returns the geo for the passed address.
*
* @param vSelf The address id
* @return The geo id
*/
DECLARE vGeoFk INT;
SELECT p.geoFk INTO vGeoFk
FROM address a
JOIN town t ON t.provinceFk = a.provinceFk
JOIN postCode p ON p.townFk = t.id AND p.`code` = a.postalCode
WHERE a.id = vSelf
ORDER BY (a.city SOUNDS LIKE t.`name`) DESC
LIMIT 1;
RETURN vGeoFk;
END$$
DELIMITER ;