对于按照字符排序,对于排序的结果值需要运算的,可能直接order by (xxx + 0)的形式可能行不通,于是调用函数 to_number(xxx),可以对于返回数字进行排序
DELIMITER $$
USE `maventest`$$
DROP FUNCTION IF EXISTS `to_number`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `to_number`(NAME VARCHAR(255) CHARSET gbk) RETURNS VARCHAR(4000) CHARSET gbk
BEGIN
DECLARE mycode INT;
DECLARE tmp_lcode VARCHAR(2) CHARSET gbk;
DECLARE lcode INT;
DECLARE mypy VARCHAR(4000) CHARSET gbk DEFAULT '';
DECLARE lp INT;
SET mycode = 0;
SET lp = 1;
SET NAME = HEX(NAME);
WHILE lp < LENGTH(NAME) DO
SET tmp_lcode = SUBSTRING(NAME, lp, 2);
SET lcode = CAST(ASCII(UNHEX(tmp_lcode)) AS UNSIGNED);
IF lcode > 128 THEN
SET lp = lp + 4;
ELSE
SET lp = lp + 2;
END IF;
SET mypy = CONCAT(mypy,lcode);
END WHILE;
RETURN LOWER(mypy);
END$$
DELIMITER ;