### SETUP

CREATE TABLE `TEST_Lookup` (
  `lookupId` int(11) NOT NULL AUTO_INCREMENT,
  `value` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`lookupId`),
  KEY `fk_lookup_value` (`value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

DELIMITER //
for i in 1..556
do
	insert into TEST_Lookup(value) values (MD5(RAND()));
end for;
//
DELIMITER ;

DELIMITER //
CREATE FUNCTION `TEST_RETURN_LOOKUP_VALUE`(LOOKUPID_IN INT) RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE LOOKUP_VALUE VARCHAR(255);

SET LOOKUP_VALUE = (SELECT `value` FROM `TEST_Lookup` WHERE `lookupId` = LOOKUPID_IN);

RETURN LOOKUP_VALUE;
END;
//
DELIMITER ;

CREATE TABLE `TEST_Table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `categoryLookupId` int(11) DEFAULT NULL,
  `date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `aap_category` (`categoryLookupId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

start transaction;
DELIMITER //
for i in 1..554293
do
	insert into TEST_Table(categoryLookupId, date) values (floor(rand()*(4-1+1))+1, NOW() - INTERVAL FLOOR(RAND() * 14 * 24 * 60 * 60) SECOND);
end for;
//
DELIMITER ;
commit;


### Query
select date,
`TEST_RETURN_LOOKUP_VALUE`(`categoryLookupId`) AS `propertyCategory`
from `TEST_Table`
order by date desc
limit 5;