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

28 lines
624 B
MySQL
Raw Permalink Normal View History

2024-11-14 08:27:38 +00:00
DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`client_getGeo`(vSelf INT)
RETURNS int(11)
DETERMINISTIC
BEGIN
/**
* Returns the geo for the passed client.
*
* @param vSelf The address id
* @return The geo id
*/
DECLARE vGeoFk INT;
SELECT p.geoFk INTO vGeoFk
FROM client c
JOIN town t ON t.provinceFk = c.provinceFk
JOIN postCode p ON p.townFk = t.id
AND (p.code = c.postcode OR c.postcode IS NULL)
2024-11-18 12:09:33 +00:00
JOIN zoneGeo zg ON zg.id = p.geoFk
2024-11-14 08:27:38 +00:00
WHERE c.id = vSelf
2024-11-18 07:58:38 +00:00
ORDER BY (c.city SOUNDS LIKE t.name) DESC,
(p.code = c.postcode) DESC
2024-11-14 08:27:38 +00:00
LIMIT 1;
RETURN vGeoFk;
END$$
DELIMITER ;