salix/db/routines/vn/functions/address_getGeo.sql

27 lines
578 B
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`address_getGeo`(vSelf INT)
RETURNS int(11)
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
2024-11-18 07:58:38 +00:00
JOIN postCode p ON p.townFk = t.id
2024-11-18 12:09:33 +00:00
JOIN zoneGeo zg ON zg.id = p.geoFk
WHERE a.id = vSelf
2024-11-18 07:58:38 +00:00
ORDER BY (a.city SOUNDS LIKE t.name) DESC,
(p.code = a.postalCode) DESC
LIMIT 1;
RETURN vGeoFk;
END$$
DELIMITER ;