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

28 lines
566 B
MySQL
Raw Normal View History

2019-09-26 08:08:37 +00:00
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 ;