利用中文拼音首字母查询数据库

时间:2022-04-17 07:50:40

1.创建表

CREATE TABLE `x_ch2en` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `ch` varchar(20) DEFAULT NULL COMMENT '中文',
  `en` varchar(20) DEFAULT NULL COMMENT '中文对应的字母',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8 COMMENT='中文对应字母表';

2.插入中英对照数据

insert   into   x_ch2en(ch,en)   
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 '

3.用 where user_name>='' and user_name <'八' 来查询中首字母为A的结果