mysql 根据英文首字母来查询汉字

时间:2021-07-27 20:13:39

表如下所示:
mysql 根据英文首字母来查询汉字

需求是在网页名称的检索框内输入“W”或“w”或“王”,可以检索出数据库中name字段汉字拼音搜字母为“w”或包含汉字为“王”的记录。

需要修改name字段的字符集为gb2313,如下图所示:
mysql 根据英文首字母来查询汉字

sql为:

select c.* from (SELECT *,
CHAR(INTERVAL(CONV(HEX(left(name,1)),16,10),
0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,
0xBBF7,0xBBF7,0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,
0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA,0xCDDA,0xCDDA,0xCEF4,
0xD1B9,0xD4D1)+64) as dxzm,CHAR(INTERVAL(CONV(HEX(left(name,1)),16,10),
0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,
0xBBF7,0xBBF7,0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,
0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA,0xCDDA,0xCDDA,0xCEF4,
0xD1B9,0xD4D1)+96) as xxzm
from employee) c where c.dxzm like '%W%' or c.xxzm like '%w%' or c.name like '%王%';

查询结果为:
mysql 根据英文首字母来查询汉字