MySQL汉字转拼音方案

1.创建表

-- sys.pinyintable definition
CREATE TABLE `pinyintable` (
  `id` int NOT NULL,
  `pin_yin` varchar(255) DEFAULT NULL,
  `code` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

2.插入数据

Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (1, 'a', '20319'),
  (2, 'ai', '20317'),
  (3, 'an', '20304'),
  (4, 'ang', '20295'),
  (5, 'ao', '20292'),
  (6, 'ba', '20283'),
  (7, 'bai', '20265'),
  (8, 'ban', '20257'),
  (9, 'bang', '20242'),
  (10, 'bao', '20230');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (11, 'bei', '20051'),
  (12, 'ben', '20036'),
  (13, 'beng', '20032'),
  (14, 'bi', '20026'),
  (15, 'bian', '20002'),
  (16, 'biao', '19990'),
  (17, 'bie', '19986'),
  (18, 'bin', '19982'),
  (19, 'bing', '19976'),
  (20, 'bo', '19805');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (21, 'bu', '19784'),
  (22, 'ca', '19775'),
  (23, 'cai', '19774'),
  (24, 'can', '19763'),
  (25, 'cang', '19756'),
  (26, 'cao', '19751'),
  (27, 'ce', '19746'),
  (28, 'ceng', '19741'),
  (29, 'cha', '19739'),
  (30, 'chai', '19728');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (31, 'chan', '19725'),
  (32, 'chang', '19715'),
  (33, 'chao', '19540'),
  (34, 'che', '19531'),
  (35, 'chen', '19525'),
  (36, 'cheng', '19515'),
  (37, 'chi', '19500'),
  (38, 'chong', '19484'),
  (39, 'chou', '19479'),
  (40, 'chu', '19467');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (41, 'chuai', '19289'),
  (42, 'chuan', '19288'),
  (43, 'chuang', '19281'),
  (44, 'chui', '19275'),
  (45, 'chun', '19270'),
  (46, 'chuo', '19263'),
  (47, 'ci', '19261'),
  (48, 'cong', '19249'),
  (49, 'cou', '19243'),
  (50, 'cu', '19242');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (51, 'cuan', '19238'),
  (52, 'cui', '19235'),
  (53, 'cun', '19227'),
  (54, 'cuo', '19224'),
  (55, 'da', '19218'),
  (56, 'dai', '19212'),
  (57, 'dan', '19038'),
  (58, 'dang', '19023'),
  (59, 'dao', '19018'),
  (60, 'de', '19006');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (61, 'deng', '19003'),
  (62, 'di', '18996'),
  (63, 'dian', '18977'),
  (64, 'diao', '18961'),
  (65, 'die', '18952'),
  (66, 'ding', '18783'),
  (67, 'diu', '18774'),
  (68, 'dong', '18773'),
  (69, 'dou', '18763'),
  (70, 'du', '18756');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (71, 'duan', '18741'),
  (72, 'dui', '18735'),
  (73, 'dun', '18731'),
  (74, 'duo', '18722'),
  (75, 'e', '18710'),
  (76, 'en', '18697'),
  (77, 'er', '18696'),
  (78, 'fa', '18526'),
  (79, 'fan', '18518'),
  (80, 'fang', '18501');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (81, 'fei', '18490'),
  (82, 'fen', '18478'),
  (83, 'feng', '18463'),
  (84, 'fo', '18448'),
  (85, 'fou', '18447'),
  (86, 'fu', '18446'),
  (87, 'ga', '18239'),
  (88, 'gai', '18237'),
  (89, 'gan', '18231'),
  (90, 'gang', '18220');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (91, 'gao', '18211'),
  (92, 'ge', '18201'),
  (93, 'gei', '18184'),
  (94, 'gen', '18183'),
  (95, 'geng', '18181'),
  (96, 'gong', '18012'),
  (97, 'gou', '17997'),
  (98, 'gu', '17988'),
  (99, 'gua', '17970'),
  (100, 'guai', '17964');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (101, 'guan', '17961'),
  (102, 'guang', '17950'),
  (103, 'gui', '17947'),
  (104, 'gun', '17931'),
  (105, 'guo', '17928'),
  (106, 'ha', '17922'),
  (107, 'hai', '17759'),
  (108, 'han', '17752'),
  (109, 'hang', '17733'),
  (110, 'hao', '17730');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (111, 'he', '17721'),
  (112, 'hei', '17703'),
  (113, 'hen', '17701'),
  (114, 'heng', '17697'),
  (115, 'hong', '17692'),
  (116, 'hou', '17683'),
  (117, 'hu', '17676'),
  (118, 'hua', '17496'),
  (119, 'huai', '17487'),
  (120, 'huan', '17482');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (121, 'huang', '17468'),
  (122, 'hui', '17454'),
  (123, 'hun', '17433'),
  (124, 'huo', '17427'),
  (125, 'ji', '17417'),
  (126, 'jia', '17202'),
  (127, 'jian', '17185'),
  (128, 'jiang', '16983'),
  (129, 'jiao', '16970'),
  (130, 'jie', '16942');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (131, 'jin', '16915'),
  (132, 'jing', '16733'),
  (133, 'jiong', '16708'),
  (134, 'jiu', '16706'),
  (135, 'ju', '16689'),
  (136, 'juan', '16664'),
  (137, 'jue', '16657'),
  (138, 'jun', '16647'),
  (139, 'ka', '16474'),
  (140, 'kai', '16470');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (141, 'kan', '16465'),
  (142, 'kang', '16459'),
  (143, 'kao', '16452'),
  (144, 'ke', '16448'),
  (145, 'ken', '16433'),
  (146, 'keng', '16429'),
  (147, 'kong', '16427'),
  (148, 'kou', '16423'),
  (149, 'ku', '16419'),
  (150, 'kua', '16412');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (151, 'kuai', '16407'),
  (152, 'kuan', '16403'),
  (153, 'kuang', '16401'),
  (154, 'kui', '16393'),
  (155, 'kun', '16220'),
  (156, 'kuo', '16216'),
  (157, 'la', '16212'),
  (158, 'lai', '16205'),
  (159, 'lan', '16202'),
  (160, 'lang', '16187');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (161, 'lao', '16180'),
  (162, 'le', '16171'),
  (163, 'lei', '16169'),
  (164, 'leng', '16158'),
  (165, 'li', '16155'),
  (166, 'lia', '15959'),
  (167, 'lian', '15958'),
  (168, 'liang', '15944'),
  (169, 'liao', '15933'),
  (170, 'lie', '15920');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (171, 'lin', '15915'),
  (172, 'ling', '15903'),
  (173, 'liu', '15889'),
  (174, 'long', '15878'),
  (175, 'lou', '15707'),
  (176, 'lu', '15701'),
  (177, 'lv', '15681'),
  (178, 'luan', '15667'),
  (179, 'lue', '15661'),
  (180, 'lun', '15659');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (181, 'luo', '15652'),
  (182, 'ma', '15640'),
  (183, 'mai', '15631'),
  (184, 'man', '15625'),
  (185, 'mang', '15454'),
  (186, 'mao', '15448'),
  (187, 'me', '15436'),
  (188, 'mei', '15435'),
  (189, 'men', '15419'),
  (190, 'meng', '15416');
Insert Into sys.pinyintable
  (ID, pin_yin, code)
Values
  (191, 'mi', '15408'),
  (192, 'mian', '15394'),
  (193, 'miao', '15385'),
  (194, 'mie', '15377'),
  (195, 'min', '15375'),
  (196, 'ming', '15369'),
  (197, 'miu', '15363'),
  (198, 'mo', '15362'),
  (199, 'mou', '15183'),
  (200, 'mu', '15180');

3.创建函数

Create Function sys.to_pinyin (NAME VARCHAR(255) CharSet gbk) RETURNS Varchar(255) CHARSet gbk
Begin
  Declare mycode INT ; 
  Declare tmp_lcode Varchar(2) CHARSet gbk ;
  Declare lcode INT ; 
  Declare tmp_rcode Varchar(2) CHARSet gbk ;
  Declare rcode INT ; 
  Declare mypy Varchar(255) CHARSet gbk DEFAULT '' ; 
  Declare lp INT ;
 
  Set mycode = 0 ;
  Set lp = 1 ;
  Set NAME = HEX(NAME) ;
 
  While lp < LENGTH(NAME) DO
    Set tmp_lcode = SUBSTRING(NAME, lp, 2) ;
    Set lcode = CAST(ASCII(UNHEX(tmp_lcode)) AS UNSIGNED) ;
    Set tmp_rcode = SUBSTRING(NAME, lp + 2, 2) ;
    Set rcode = CAST(ASCII(UNHEX(tmp_rcode)) AS UNSIGNED) ;
 
    IF lcode > 128 THEN
      Set mycode = 65536 - lcode * 256 - rcode ; 
      Select CONCAT(mypy, pin_yin) Into mypy From sys.pinyintable Where code >= Abs(mycode) Order By code Asc Limit 1;
      Set lp = lp + 4 ;
    Else
      Set mypy = Concat(mypy,Char(Cast(Ascii(UNHEX(SUBSTRING(NAME, lp, 2))) AS UNSIGNED)));
      Set lp = lp + 2 ;
    End If;
  End While;
  Return Lower(mypy) ;
End;

Select sys.to_pinyin("中国") From Dual;
--结果
zhongguo
相关推荐
Karoku06618 分钟前
【企业级分布式系统】ELK优化
运维·服务器·数据库·elk·elasticsearch
小技与小术1 小时前
数据库表设计范式
数据库·mysql
安迁岚2 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验三 数据操作
运维·服务器·数据库·sql·mysql
安迁岚2 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验九 触发器
数据库·sql·mysql·oracle·实验报告
Loganer2 小时前
MongoDB分片集群搭建
数据库·mongodb
LKID体2 小时前
Python操作neo4j库py2neo使用之创建和查询(二)
数据库·python·neo4j
刘大浪2 小时前
后端数据增删改查基于Springboot+mybatis mysql 时间根据当时时间自动填充,数据库连接查询不一致,mysql数据库连接不好用
数据库·spring boot·mybatis
一只爱撸猫的程序猿2 小时前
简单实现一个系统升级过程中的数据平滑迁移的场景实例
数据库·spring boot·程序员
无敌岩雀2 小时前
MySQL中的索引
数据库·mysql
a_安徒生3 小时前
linux安装TDengine
linux·数据库·tdengine