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
相关推荐
q***87607 小时前
yum安装redis
数据库·redis·缓存
IT油腻大叔7 小时前
MySQL VS ClickHouse 索引结构对比分析
mysql·clickhouse
煎蛋学姐7 小时前
SSM旅游资讯信息服务系统的实现04s3n(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·旅游·ssm 框架·旅游资讯系统·会员管理
3***31217 小时前
初识MySQL · 库的操作
数据库·mysql
合作小小程序员小小店7 小时前
网页开发,在线%新版本旅游管理%系统,基于eclipse,html,css,jquery,servlet,jsp,mysql数据库
java·数据库·eclipse·html·intellij-idea·旅游·jsp
anod7 小时前
奇怪的mysql时区问题
数据库·mysql·eclipse
鲸说MySQL7 小时前
MySQL表文件损坏
数据库·mysql
a***59267 小时前
【SQL技术】不同数据库引擎 SQL 优化方案剖析
数据库·sql
0***v7777 小时前
使用Dify访问数据库(mysql)
数据库·mysql
愚戏师8 小时前
MySQL 数据导出
数据库·笔记·mysql