CREATE FUNCTION hzcode (s CHAR(255)) RETURNS char
BEGIN
DECLARE hz_code int;
DECLARE hz_py char;
SET hz_code = ord(substring(s,1,1))*256+ord(substring(s,2,1))-65536 ;
select py into hz_py from pyk where hz_code>=pyk.hz1 and hz_code<=pyk
.hz2;
RETURN hz_py;
END
//
delimiter $$
DROP FUNCTION IF EXISTS `hzcode`$$
CREATE FUNCTION `hzcode` (s CHAR(255)) RETURNS char
BEGIN
DECLARE hz_code int;
DECLARE hz_py char;
declare str varchar(400);
SET hz_code = ord(substring(s,1,1))-65536 ;
select py into hz_py from pyk where hz_code>=pyk.hz1 and hz_code<=pyk.hz2;
RETURN hz_py;
END$$
delimiter $$
3、先测试一下
mysql> select hzcode('南海龙王');
+--------------------+
| hzcode('南海龙王') |
+--------------------+
| N |
+--------------------+
1 row in set (0.00 sec)
4、建立个测试表
DROP TABLE IF EXISTS `f1`;
create table f1 (
name varchar(30),
pykey varchar(1)
);