在mysql中没有系统自带的函数可以实现查找中文字符的第一个汉字的首字母了,在这里我整理了两种比较好用的方法,下面一起来看看吧.
例子1:mysql功能函数,实现拼音查询.
功能:输入中文字符串每个字的首字母,即可检索出相应数据.
使用方法:直接使用py(字段名)=‘keywords’,即可,代码如下:
- DELIMITER$$
- CREATEFUNCTION`PYFIRST`(P_NAMEVARCHAR(255))RETURNSvarchar(255)CHARSETutf8
- BEGIN
- DECLAREV_RETURNVARCHAR(255);
- SETV_RETURN=ELT(INTERVAL(CONV(HEX(left(CONVERT(P_NAMEUSINGgbk),1)),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),'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');
- RETURNV_RETURN;
- END$$
- DELIMITER;
- DELIMITER$$
- CREATEFUNCTION`PY`(P_NAMEVARCHAR(255))RETURNSvarchar(255)CHARSETutf8
- BEGIN
- DECLAREV_COMPAREVARCHAR(255);
- DECLAREV_RETURNVARCHAR(255);
- DECLAREIINT;
- SETI=1;
- SETV_RETURN='';
- whileI<LENGTH(P_NAME)do
- SETV_COMPARE=SUBSTR(P_NAME,I,1);
- IF(V_COMPARE!='')THEN
- #SETV_RETURN=CONCAT(V_RETURN,',',V_COMPARE);
- SETV_RETURN=CONCAT(V_RETURN,PYFIRST(V_COMPARE));
- #SETV_RETURN=PYFIRST(V_COMPARE);
- ENDIF;
- SETI=I+1;
- endwhile;
- IF(ISNULL(V_RETURN)orV_RETURN='')THEN
- SETV_RETURN=P_NAME;
- ENDIF;
- RETURNV_RETURN;
- END$$
- DELIMITER;
例子2,现在给出在mysql 里实现的,测试环境是mysql-5.0.27-win32.
1、建立拼音首字母资料表,代码如下:
- DROPTABLEIFEXISTS`pyk`;
- CREATETABLE`pyk`(
- `PY`varchar(1),
- `HZ1`int,
- `HZ2`int
- );
- INSERTINTO`pyk`(`PY`,`HZ1`,`HZ2`)VALUES
- ('A',-20319,-20284),
- ('B',-20283,-19776),
- ('C',-19775,-19219),
- ('D',-19218,-18711),
- ('E',-18710,-18527),
- ('F',-18526,-18240),
- ('G',-18239,-17923),
- ('H',-17922,-17418),
- ('J',-17417,-16475),
- ('K',-16474,-16213),
- ('L',-16212,-15641),
- ('M',-15640,-15166),
- ('N',-15165,-14923),
- ('O',-14922,-14915),
- ('P',-14914,-14631),
- ('Q',-14630,-14150),
- ('R',-14149,-14091),
- ('S',-14090,-13319),
- ('T',-13318,-12839),
- ('W',-12838,-12557),
- ('X',-12556,-11848),
- ('Y',-11847,-11056),
- ('Z',-11055,-10247);
2、建立mysql 函数,代码如下:
- DROPFUNCTIONIFEXISTShzcode;
- delimiter//
- CREATEFUNCTIONhzcode(sCHAR(255))RETURNSchar
- BEGIN
- DECLAREhz_codeint;
- DECLAREhz_pychar;
- SEThz_code=ord(substring(s,1,1))*256+ord(substring(s,2,1))-65536;
- selectpyintohz_pyfrompykwherehz_code>=pyk.hz1andhz_code<=pyk
- .hz2;
- RETURNhz_py;
- END
- //
delimiter ;
数据库类型如果是GBK的时候,这个函数好像有点小问题,稍微改动了下,貌似可以在GBK中使用了,代码如下:
- delimiter$$
- DROPFUNCTIONIFEXISTS`hzcode`$$
- CREATEFUNCTION`hzcode`(sCHAR(255))RETURNSchar
- BEGIN
- DECLAREhz_codeint;
- DECLAREhz_pychar;
- declarestrvarchar(400);
- SEThz_code=ord(substring(s,1,1))-65536;
- selectpyintohz_pyfrompykwherehz_code>=pyk.hz1andhz_code<=pyk.hz2;
- RETURNhz_py;
- END$$
- delimiter$$
3、先测试一下
- mysql>selecthzcode('南海龙王');
- +--------------------+
- |hzcode('南海龙王')|
- +--------------------+
- |N|
- +--------------------+
- 1rowinset(0.00sec)
4、建立个测试表
- DROPTABLEIFEXISTS`f1`;
- createtablef1(
- namevarchar(30),
- pykeyvarchar(1)
- );
- insertintof1(name)values
- ('张三'),
- ('李四'),
- ('王五'),
- ('赵六'),
- ('钱七');
5、测试
- mysql>select*fromf1;
- +------+-------+
- |name|pykey|
- +------+-------+
- |张三|NULL|
- |李四|NULL|
- |王五|NULL|
- |赵六|NULL|
- |钱七|NULL|
- +------+-------+
- 5rowsinset(0.00sec)
- mysql>updatef1setpykey=hzcode(name);
- QueryOK,5rowsaffected(0.05sec)
- Rowsmatched:5Changed:5Warnings:0
- mysql>select*fromf1;//开源代码phpfensi.com
- +------+-------+
- |name|pykey|
- +------+-------+
- |张三|Z|
- |李四|L|
- |王五|W|
- |赵六|Z|
- |钱七|Q|
- +------+-------+
- 5rowsinset(0.00sec)
这样就很方便地在MYSQL里查询汉字的首字母了,类似地也可以直接在MYSQL得到汉字拼音,不过需要拼音表,函数写法也不一样.