MYSQL 汉字转拼音首字母(支持复杂字)

首先在MySQL 中创建 首字母的函数

DELIMITER $$  

CREATE  
    /*[DEFINER = { user | CURRENT_USER }]*/  
    FUNCTION `表名`.`GET_FIRST_PINYIN_CHAR_1`(PARAM VARCHAR(255))RETURNS VARCHAR(2) CHARSET utf8    

    BEGIN  
 DECLARE V_RETURN VARCHAR(255);  
    DECLARE V_FIRST_CHAR VARCHAR(2);  
    SET V_FIRST_CHAR = UPPER(LEFT(PARAM,1));  
    SET V_RETURN = V_FIRST_CHAR;  
    IF LENGTH( V_FIRST_CHAR) <> CHARACTER_LENGTH( V_FIRST_CHAR ) THEN  
    SET V_RETURN = ELT(INTERVAL(CONV(HEX(LEFT(CONVERT(PARAM USING gbk),1)),16,10),  
        0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,  
        0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,  
        0xC8F6,0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1),  
    'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');  
    END IF;  
    RETURN V_RETURN;  
    END$$  

DELIMITER ;

然后使用刚刚定义的 GET_FIRST_PINYIN_CHAR_1进行搜索

SELECT full_name,get_first_pinyin_char_1(full_name) FROM sys_user where get_first_pinyin_char_1(full_name) = 'G';

查询结果
MYSQL 汉字转拼音首字母(支持复杂字)_第1张图片

多个拼音首字母

DELIMITER $$

USE `cdpathdatabase`$$

DROP FUNCTION IF EXISTS `getPY`$$

CREATE DEFINER=`root`@`%` FUNCTION `getPY`(in_string VARCHAR(21845)) RETURNS VARCHAR(21845) CHARSET utf8
BEGIN
#截取字符串,每次做截取后的字符串存放在该变量中,初始为函数参数in_string值
DECLARE tmp_str VARCHAR(21845) CHARSET gbk DEFAULT '' ; 
#tmp_str的长度
DECLARE tmp_len SMALLINT DEFAULT 0;
#tmp_str的长度
DECLARE tmp_loc SMALLINT DEFAULT 0;
#截取字符,每次 left(tmp_str,1) 返回值存放在该变量中
DECLARE tmp_char VARCHAR(2) CHARSET gbk DEFAULT '';
#结果字符串
DECLARE tmp_rs VARCHAR(21845)CHARSET gbk DEFAULT '';
#拼音字符,存放单个汉字对应的拼音首字符
DECLARE tmp_cc VARCHAR(2) CHARSET gbk DEFAULT '';
#初始化,将in_string赋给tmp_str
SET tmp_str = in_string;
#初始化长度
SET tmp_len = LENGTH(tmp_str);
#如果被计算的tmp_str长度大于0则进入该while
WHILE tmp_len > 0 DO 
#获取tmp_str最左端的首个字符,注意这里是获取首个字符,该字符可能是汉字,也可能不是。
SET tmp_char = LEFT(tmp_str,1);
#左端首个字符赋值给拼音字符
SET tmp_cc = tmp_char;
#获取字符的编码范围的位置,为了确认汉字拼音首字母是那一个
SET tmp_loc=INTERVAL(CONV(HEX(tmp_char),16,10),0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,0xBFA6,0xC0AC
,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA ,0xCEF4,0xD1B9,0xD4D1);
#判断左端首个字符是多字节还是单字节字符,要是多字节则认为是汉字且作以下拼音获取,要是单字节则不处理。如果是多字节字符但是不在对应的编码范围之内,即对应的不是大写字母则也不做处理,这样数字或者特殊字符就保持原样了
IF (LENGTH(tmp_char)>1 AND tmp_loc>0 AND tmp_loc<24) THEN
#获得汉字拼音首字符
SELECT ELT(tmp_loc,'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z') INTO tmp_cc; 
END IF;
#将当前tmp_str左端首个字符拼音首字符与返回字符串拼接
SET tmp_rs = CONCAT(tmp_rs,tmp_cc);
#将tmp_str左端首字符去除
SET tmp_str = SUBSTRING(tmp_str,2);
#计算当前字符串长度
SET tmp_len = LENGTH(tmp_str);
END WHILE;
#返回结果字符串
RETURN tmp_rs;
END$$

DELIMITER ;

然后查询

SELECT full_name,getPY(full_name) FROM cdpathdatabase.sys_user;

结果图
MYSQL 汉字转拼音首字母(支持复杂字)_第2张图片

不过这种有些复杂点的字就识别不出,比如睾丸 ,膀胱
以下能解决这种问题但识别不了多音字 毕竟多音字太多了

DELIMITER $$
USE `cdpathdatabase`$$
CREATE DEFINER=`root`@`%` FUNCTION `F_GETPY`(in_string VARCHAR(100)) RETURNS varchar(100) CHARSET utf8
BEGIN	
DECLARE tmp_str VARCHAR(100) CHARSET gbk DEFAULT '' ;
DECLARE tmp_char VARCHAR(100) CHARSET gbk DEFAULT '' ; 
DECLARE V_LEN INT;
DECLARE V_I INT;
DECLARE V_PY VARCHAR(100);
SET V_LEN=CHAR_LENGTH(in_string);
SET V_I=1;
SET V_PY='';
DROP TEMPORARY TABLE IF EXISTS TT_PYZD;
CREATE TEMPORARY TABLE TT_PYZD (chr char(2) ,letter char(2)) DEFAULT CHARSET gbk;
INSERT INTO TT_PYZD
SELECT '吖 ', 'A ' UNION ALL SELECT '八 ', 'B ' UNION ALL 
SELECT '嚓 ', 'C ' UNION ALL SELECT '咑 ', 'D ' UNION ALL 
SELECT '妸 ', 'E ' UNION ALL SELECT '发 ', 'F ' UNION ALL 
SELECT '旮 ', 'G ' UNION ALL SELECT '铪 ', 'H ' UNION ALL 
SELECT '丌 ', 'J ' UNION ALL SELECT '咔 ', 'K ' UNION ALL 
SELECT '垃 ', 'L ' UNION ALL SELECT '嘸 ', 'M ' UNION ALL 
SELECT '拏 ', 'N ' UNION ALL SELECT '噢 ', 'O ' UNION ALL 
SELECT '妑 ', 'P ' UNION ALL SELECT '七 ', 'Q ' UNION ALL 
SELECT '呥 ', 'R ' UNION ALL SELECT '仨 ', 'S ' UNION ALL 
SELECT '他 ', 'T ' UNION ALL SELECT '屲 ', 'W ' UNION ALL 
SELECT '夕 ', 'X ' UNION ALL SELECT '丫 ', 'Y ' UNION ALL 
SELECT '帀 ', 'Z ' ;
WHILE V_I<=V_LEN DO 
SET tmp_str = substring(in_string,V_I,1);
IF ASCII(tmp_str)<127 
THEN
SET tmp_char=UPPER(tmp_str);
ELSE
SELECT letter INTO tmp_char FROM TT_PYZD WHERE chr<=tmp_str ORDER BY chr DESC LIMIT 1;
END IF ;
SET V_I=V_I+1;
SET V_PY=CONCAT(V_PY,tmp_char);
END WHILE;
DROP TEMPORARY TABLE IF EXISTS TT_PYZD;
RETURN V_PY;
END;

你可能感兴趣的:(mysql)