MySQL 瀵瑰瓧绗︽帓搴忥紝绫婚�氳褰�

1. 瑕佹眰

绗竴灞傛帓搴忥細浠ラ瀛楃涓烘爣鍑嗭紝銆怉-Z锛岀壒娈婄鍙穦鏁板瓧|emoji銆戝垎绫伙紝棣栧瓧绗︽槸涓枃鍒欒浆澹版瘝鑻辨枃銆�
绗簩灞傛帓搴忥細澶х被涓A涓殑鎵�鏈夊瓧绗︽寜GBK缂栫爜鎺掑簭
绗笁灞傛帓搴忥細澶у啓鐨勬暟瀛椼�愪竴浜屼笁鍥涗簲鍏竷鍏節鍗併�戞寜鏁板瓧銆�12345678910銆戞帓搴�

2. 瀹炵幇

GBK涓瀛楃涓蹭腑鐨勬瘡浣嶅瓧绗︾殑鎺掑簭鏄� 銆愮┖|鏁板瓧|鐗规畩瀛楃|emoji|鑻辨枃澶у皬鍐欍�戯紝棣栧瓧绗︾殑鎺掑簭闇�瑕佽皟鏁磋繖涓『搴忎负銆愯嫳鏂囧ぇ灏忓啓|鏁板瓧|鐗规畩瀛楃|emoji銆戙��
棣栧厛绗竴浣嶆槸涓枃鎹㈡垚澹版瘝鑻辨枃銆傚涓媘ysql鍑芥暟

CREATE FUNCTION `getFirstCode`(in_string VARCHAR(255)) RETURNS varchar(255) CHARSET utf8mb4
    DETERMINISTIC
    COMMENT '鑾峰彇涓枃棣栧瓧姣嶅嚱鏁�'
BEGIN

#瀹氫箟涓存椂瀛楃涓插彉閲忥紝鐢ㄤ簬鎺ユ敹鍑芥暟涓紶閫掕繘鏉ョ殑瀛楃涓插�硷紝杩欓噷鏄痠n_string
DECLARE tmp_str VARCHAR(255) CHARSET gbk DEFAULT '' ; 

#瀹氫箟涓存椂瀛楃涓插彉閲忥紝鐢ㄤ簬瀛樻斁鍑芥暟涓紶閫掕繘鏉ョ殑瀛楃涓插�肩殑绗竴涓瓧绗�
DECLARE tmp_char VARCHAR(2) CHARSET gbk DEFAULT '';

#tmp_str鐨勯暱搴�
DECLARE tmp_loc SMALLINT DEFAULT 0;

#鍒濆鍖栵紝灏唅n_string璧嬬粰tmp_str
SET tmp_str = in_string;

#鑾峰彇tmp_str鏈�宸︾鐨勯涓瓧绗︼紝娉ㄦ剰杩欓噷鏄幏鍙栭涓瓧绗︼紝璇ュ瓧绗﹀彲鑳芥槸姹夊瓧锛屼篃鍙兘涓嶆槸銆�
SET tmp_char = LEFT(tmp_str,1);

#鑾峰彇瀛楃鐨勭紪鐮佽寖鍥寸殑浣嶇疆锛屼负浜嗙‘璁ゆ眽瀛楁嫾闊抽瀛楁瘝鏄偅涓�涓紝瓒呰繃鎸夐瀛楁瘝鎺掑簭鐨勯儴鍒嗭紝浣跨敤绗竴涓狦BK鍐呯爜0xD8A1

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,0xD8A1);

#鍒ゆ柇宸︾棣栦釜瀛楃鏄瀛楄妭杩樻槸鍗曞瓧鑺傚瓧绗︼紝瑕佹槸澶氬瓧鑺傚垯璁や负鏄眽瀛椾笖浣滀互涓嬫嫾闊宠幏鍙栵紝瑕佹槸鍗曞瓧鑺傚垯涓嶅鐞嗐��
#濡傛灉鏄瀛楄妭瀛楃浣嗘槸涓嶅湪瀵瑰簲鐨勭紪鐮佽寖鍥翠箣鍐咃紝鍗冲搴旂殑涓嶆槸澶у啓瀛楁瘝鍒欎篃涓嶅仛澶勭悊锛岃繖鏍锋暟瀛楁垨鑰呯壒娈婂瓧绗﹀氨淇濇寔鍘熸牱浜�

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_char; #鑾峰緱姹夊瓧鎷奸煶棣栧瓧绗�
ELSEIF(tmp_loc>=24) THEN
  SELECT left(pinyin,1) INTO tmp_char FROM base_hanzi WHERE hanzi = tmp_char;
END IF;

RETURN tmp_char;#杩斿洖姹夊瓧鎷奸煶棣栧瓧绗�
END

闇�瑕佹敞鎰忥細

GBK缂栫爜锛屾槸瀵笹B2312缂栫爜鐨勬墿灞曪紝鍥犳瀹屽叏鍏煎GB2312-80鏍囧噯銆�

GB2312瀵规眽瀛楃殑缂栫爜鎺掑簭涔熸槸鎸夌収A-Z鐨勯『搴忓垎缁勭紪鐮侊紝鍙湁鎵惧埌涓寸晫鍊硷紝閫氳繃GBK鍐呯爜锛堝崄鍏繘鍒讹級涓庝复鐣屽�肩殑浣嶇疆锛屽氨鍙緱鍑哄0姣嶆槸'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z'鐨勫摢涓�涓紝鍙槸杩欓噷鏈変釜闂锛孏B2312鍙槸瀵归儴鍒嗘眽瀛楄繘琛岀紪鐮侊紝杩樻湁寰堝甯歌瀛楁病鏈夛紝GBK瀵规鎵╁睍鍚庯紝鏄寜鐓у叾浠栫殑缂栨帓椤哄簭锛屼娇鐢ㄨ繖涓柟娉曡绠楀嚭鐨勫0姣嶏紝浼氭湁閿欒銆�

鍘熸潵姹夊瓧鐨勭紪鐮佸彧鏈塆B 2312锛屽彧鍖呭惈6763涓眽瀛楋紝GBK瀹屽叏鍏煎浜咷B 2312鐨勫唴瀹瑰拰椤哄簭锛屽苟澧炲姞浜嗕竴涓囧涓眽瀛楋紝浣嗘槸椤哄簭鏄寜銆婂悍鐔欏瓧鍏搞�嬬殑椤电爜/瀛椾綅鎺掑垪锛岄偅浜涙柊澧炵殑姹夊瓧锛岀紪鐮佸�奸兘姣� D7F9->搴э紙鏈�鍚庝竴涓湁杩欒寰嬬殑姹夊瓧锛� 澶э紝鎵�浠ラ兘璇嗗埆鎴愪簡Z銆�

鍦ㄦ铏氭瀯绗�24涓0姣嶏紝鍗犱綅绗�"鏃�"锛岀劧鍚庨�氳繃鏌ヨ〃鐨勬柟寮忥紝鍘荤‘瀹氬0姣嶏紝闇�鎶婄浉鍏崇殑瀵圭収琛ㄥ瓨鍒版暟鎹簱涓鏈緥涓殑base_hanzi锛岀劧鍚庡幓鏌ヨ鑾峰彇锛屽綋鐒跺彲浠ュ叏閮ㄩ�氳繃鏁版嵁琛ㄦ潵鑾峰彇澹版瘝锛堝鏋滀腑鑻辨枃瀵圭収琛ㄨ冻澶熷噯纭拰鍏ㄩ潰锛夛紝浣嗘槸鑰冭檻鍒版�ц兘闂褰撲笉灞炰簬瑙勫垯鑼冨洿鍐呯殑姹夊瓧鍐嶅幓鏁版嵁琛ㄤ腑鏌ヨ銆�
涓枃杞负澹版瘝瀛楃鍚庯紝灏卞彲浠ュ拰鑻辨枃娣峰悎鎺掑簭浜嗐�傚涓媠ql锛�

-- 鎷奸煶z鎺掑簭鏈�鍚庣殑姹夊瓧:绯� utf8缂栫爜涓� \xe7\xb3\xb3(鍗�0xe7b3b3),gbk缂栫爜涓築C64
-- 0x41 鍗冲崄杩涘埗65 澶у啓瀛楁瘝A
 -- 0xf09f9880 琛ㄧず emoji 琛ㄦ儏馃榾
SELECT str_name,
convert(
  UNHEX(
    CASE
       WHEN HEX(LEFT(str_name, 1)) < HEX(0x41)
       THEN HEX(0xe7b3b3) -- 濡傛灉鏄皯浜庤嫳鏂囧瓧姣嶇紪鐮佸垯杞崲瀵瑰簲鐨勬眽瀛� "绯�"
       WHEN LEFT(str_name, 1) >= 0xf09f9880
       THEN HEX(0xe7b3b3)  --灏唀moji琛ㄦ儏杞崲瀵瑰簲鐨勬眽瀛� "绯�"
       WHEN HEX(LEFT(str_name, 1)) > HEX("z")
       THEN HEX(getFirstCode(str_name)) -- 姹夊瓧鑾峰彇澹版瘝
       ELSE HEX(LEFT(str_name,1))
    END
  )
  USING utf8
) 
as HanZi

from test_name
ORDER BY
CONVERT(HanZi USING GBK) ASC, -- 瀹炵幇棣栧瓧姣嶈嫳鏂噟鏁板瓧|瀛楃|emoji
CONVERT(replaceNumber(str_name) USING GBK) COLLATE gbk_chinese_ci  -- 鍏朵粬浣嶆暟鎸夌収GBK鎺掑簭
;

瀵逛簬銆愪竴浜屼笁鍥涗簲鍏竷鍏節鍗併�戝湪GBK鐨勬帓搴忎腑锛屾槸鎸夊0姣嶇殑鑻辨枃瀛楁瘝琛ㄧ殑椤哄簭锛屾澶勶紝鍐欎竴涓嚱鏁板幓鐗规畩澶勭悊锛岃浆鎹㈡垚銆�123456789a銆戯紝灏卞彲浠ユ湁鏁板瓧鐨勯『搴忎簡銆�

CREATE  FUNCTION `replaceNumber`(in_string VARCHAR(255)) RETURNS varchar(255) CHARSET utf8mb4
BEGIN

    DECLARE tmp_char VARCHAR(255) DEFAULT '';
    SET tmp_char = in_string;
  
  IF(tmp_char REGEXP '[涓�浜屼笁鍥涗簲鍏竷鍏節鍗乚+') THEN
     SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
        tmp_char, '涓�','1'), '浜�','2'), '涓�','3'), '鍥�','4'), '浜�','5'), '鍏�','6'), '涓�','7'), '鍏�','8'), '涔�','9'), '鍗�','a')
     INTO tmp_char;
  END IF;

    RETURN tmp_char;

END

3. 璧勬簮

GBK 姹夊瓧鍐呯爜鎵╁睍瑙勮寖缂栫爜琛�(浜�)
http://ff.163.com/newflyff/gbk-list/
姹夊瓧鎷奸煶琛�
閾炬帴锛�https://pan.baidu.com/s/1JlBsC0I8LfF6YJoxvYRb9w
鎻愬彇鐮侊細wjjk

鍙傝�冿細
https://juejin.im/post/5def87c6f265da3397729ac3
https://www.jianshu.com/p/29243714a9d3

你可能感兴趣的:(MySQL 瀵瑰瓧绗︽帓搴忥紝绫婚�氳褰�)