转【数据库-MySql】一键将表中的A字段转换成拼音码到B字段 (无需中间表)

来源:转载

MySQL 数据库汉字转换拼音码

#CONVERT(expr USING transcoding_name)提供一个在不同字符集之间转换数据的方法#LEFT(str,len) 返回从字符串str 开始的len 最左字符。#HEX(char)导出时采用HEX函数读取数据,把二进制的数据转为16进制的字符串;#CONV(N,from_base,to_base) 将N从from_base进制转换成to_base进制#INTERVAL(m,N1,N2,N3...Nn) 【N1~Nn】是个数组,如果 m<Nn 返回N(n-1)的在【N1~Nn】索引 ;#ELT(m,N1,N2,N3...Nn) 【N1~Nn】是个数组,n为【N1~Nn】数组的索引,返回值为 Nm;#LENGTH(str) 字符串长度#SUBSTR(str,start,len) 裁剪字符串#CONCAT(str1,str2,str3...strn) 连接字符串#set global log_bin_trust_function_creators=true;#set global log_bin_trust_function_creators=on; set global log_bin_trust_function_creators=1;DROP FUNCTION IF EXISTS PYCOV; CREATE FUNCTION `PYCOV`(P_NAME VARCHAR(255)) RETURNS varchar(255) CHARSET gbk BEGIN DECLARE V_COMPARE VARCHAR(255); DECLARE V_RETURN VARCHAR(255); DECLARE V_GBKSTR VARCHAR(255); DECLARE V_CHAR VARCHAR(10); DECLARE I_HEX VARCHAR(10); DECLARE I_DECIMAL INT; DECLARE I_INDEX INT; DECLARE V_COVCHAR VARCHAR(10); DECLARE I INT; SET I = 1; SET V_RETURN = ''; SET V_GBKSTR = ''; SET V_CHAR = ''; SET I_HEX =''; SET I_DECIMAL = 1; SET I_INDEX = 1; SET V_COVCHAR = ''; while I < LENGTH(P_NAME) do SET V_COMPARE = SUBSTR(P_NAME, I, 1); IF (V_COMPARE != '') THEN #将字符串转换成GBK编码格式 SET V_GBKSTR = CONVERT(V_COMPARE USING gbk); #获取字符的第一个编码 SET V_CHAR = LEFT(V_GBKSTR,1); #将字符转存编码 SET I_HEX = HEX(V_CHAR); #16字符串进制转10进制 SET I_DECIMAL = CONV(I_HEX,16,10); #GBK 编码组 - 获取【汉字拼音首字母组】索引 SET I_INDEX = INTERVAL(I_DECIMAL, 0x0000, #ASCII 【0-9】,【:[email protected]】,【A-Z】, 【[-`】 【a-z】,【{-/】 0x0030,0x003A,0x0041,0x005B,0x0061,0x007B, #GBK 【0-9】,【:[email protected]】,【A-Z】, 【[-`】 【a-z】,【{-盃】 0xA3B0,0xA3BA,0xA3C1,0xA3DB,0xA3E1,0xA3FB, #GBK 汉字拼音首字母 a-h #【啊-澳】【芭-怖】【擦-错】【搭-堕】【蛾-贰】【发-咐】【噶-过】【哈-祸】 0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE, #GBK 汉字拼音首字母 j-q #【击-骏】【喀-阔】【垃-络】【妈-穆】【拿-诺】【哦-沤】【啪-瀑】【期-群】 0xBBF7,0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA, #GBK 汉字拼音首字母 r-z #【然-弱】【撒-所】【塌-唾】【挖-误】【昔-迅】【压-孕】【匝-座】【□-□】 0xC8BB,0xC8F6,0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1,0xD7FA); #汉字拼音首字母组 SET V_COVCHAR = ELT(I_INDEX, '', V_CHAR,'',V_CHAR,'',V_CHAR,'', V_CHAR,'',V_CHAR,'',V_CHAR,'', 'A','B','C','D','E','F','G','H', 'J','K','L','M','N','O','P','Q', 'R','S','T','W','X','Y','Z','' ); SET V_RETURN = CONCAT(V_RETURN, V_COVCHAR); END IF; SET I = I + 1; end while; IF (ISNULL(V_RETURN) or V_RETURN = '') THEN SET V_RETURN = P_NAME; END IF; RETURN V_RETURN; END;#根据实际情况修改表名和代码#表:medicine#源码-汉字字符串:med_name_ch#拼音码:med_py_code UPDATE medicine_copy set med_py_code = PYCOV(med_name_ch);#删除函数 DROP FUNCTION IF EXISTS PYCOV;#set global log_bin_trust_function_creators=false;#set global log_bin_trust_function_creators=off; set global log_bin_trust_function_creators=0;

分享给朋友:
您可能感兴趣的文章:
随机阅读: