DROP TABLE IF EXISTS ' Pyk '; CREATE TABLE ' Pyk ' ( ' PY ' varchar (1), ' HZ1 ' int, ' HZ2 ' int ) ;
INSERT into ' pyk ' (' PY ', ' HZ1 ', ' HZ2 ') VALUES (' A ', -20319,-20284), (' B ', -20283,-19776), (' C ', -19775,-19219), (' D ', -19218,-18711), (' E ', -18710,-18527), (' F ', -18526,-18240), (' G ', -18239,-17923), (' H ', -17922,-17418), (' J ', -17417,-16475), (' K ', -16474,-16213), (' L ', -16212,-15641), (' M ', -15640,-15166), (' N ', -15165,-14923), (' O ', -14922,-14915), (' P ', -14914,-14631), (' Q ', -14630,-14150), (' R ', -14149,-14091), (' S ', -14090,-13319), (' T ', -13318,-12839), (' W ', -12838,-12557), (' X ', -12556,-11848), (' Y ', -11847,-11056), (' Z ', -11055,-10247); 2. Set up MySQL function DROP FUNCTION IF EXISTS hzcode;
Delimiter//
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; Database type if it is GBK, this function seems to have a little problem, slightly changed, seemingly can be used in the GBK 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, First Test Mysql> Select Hzcode (' Nanhai Dragon King '); +--------------------+ | Hzcode (' Nanhai Dragon King ') | +--------------------+ | N | +--------------------+ 1 row in Set (0.00 sec) 4, set up a test table DROP TABLE IF EXISTS ' F1 '; CREATE TABLE F1 ( Name varchar (30), Pykey varchar (1) );
Insert into F1 (name) values (' John '), (' Dick '), (' Harry '), (' Zhao Liu '), (' Money seven '); 5, testing Mysql> SELECT * from F1; +------+-------+ | name | Pykey | +------+-------+ | John | NULL | | Dick | NULL | | Harry | NULL | | Zhao Liu | NULL | | Money Seven | NULL | +------+-------+ 5 rows in Set (0.00 sec) Mysql> Update f1 Set Pykey = Hzcode (name); Query OK, 5 rows affected (0.05 sec) Rows Matched:5 Changed:5 warnings:0 Mysql> SELECT * from F1; +------+-------+ | name | Pykey | +------+-------+ | John | Z | | Dick | L | | Harry | W | | Zhao Liu | Z | | Money Seven | Q | +------+-------+ 5 rows in Set (0.00 sec) |