在mysql中沒有系統自帶的函數可以實現查找中文字符的第一個漢字的首字母了,在這裡我整理了兩種比較好用的方法,下面一起來看看吧。
例子1
mysql功能函數 實現拼音查詢:
功能:輸入中文字符串每個字的首字母,即可檢索出相應數據。
使用方法:直接使用py(字段名)=‘keywords’,即可.
代碼如下
復制代碼
DELIMITER $$
CREATE FUNCTION `PYFIRST`(P_NAME VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE V_RETURN VARCHAR(255);
SET V_RETURN = ELT(INTERVAL(CONV(HEX(left(CONVERT(P_NAME USING gbk),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');
RETURN V_RETURN;
END$$
DELIMITER ;
DELIMITER $$
CREATE FUNCTION `PY`(P_NAME VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE V_COMPARE VARCHAR(255);
DECLARE V_RETURN VARCHAR(255);
DECLARE I INT;
SET I = 1;
SET V_RETURN = '';
while I < LENGTH(P_NAME) do
SET V_COMPARE = SUBSTR(P_NAME, I, 1);
IF (V_COMPARE != '') THEN
#SET V_RETURN = CONCAT(V_RETURN, ',', V_COMPARE);
SET V_RETURN = CONCAT(V_RETURN, PYFIRST(V_COMPARE));
#SET V_RETURN = PYFIRST(V_COMPARE);
END IF;
SET I = I + 1;
end while;
IF (ISNULL(V_RETURN) or V_RETURN = '') THEN
SET V_RETURN = P_NAME;
END IF;
RETURN V_RETURN;
END$$
DELIMITER ;
例子2
現在給出在mysql 裡實現的, 測試環境是mysql-5.0.27-win32
1、建立拼音首字母資料表
代碼如下
復制代碼
DROP TABLE IF EXISTS `pyk`;
CREATE TABLE `pyk` (
`PY` varchar(1) ,
`HZ1` int ,
`HZ2` int
) ;
INSERT INTO `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 函數
DROP FUNCTION IF EXISTS hzcode;
delimiter //
CREATE FUNCTION hzcode (s CHAR(255)) RETURNS char
BEGIN
DECLARE hz_code int;
DECLARE hz_py char;
SET hz_code = ord(substring(s,1,1))*256+ord(substring(s,2,1))-65536 ;
select py into hz_py from pyk where hz_code>=pyk.hz1 and hz_code<=pyk
.hz2;
RETURN hz_py;
END
//
delimiter ;
數據庫類型如果是GBK的時候,這個函數好像有點小問題,稍微改動了下,貌似可以在GBK中使用了
delimiter $$
DROP FUNCTION IF EXISTS `hzcode`$$
CREATE FUNCTION `hzcode` (s CHAR(255)) RETURNS char
BEGIN
DECLARE hz_code int;
DECLARE hz_py char;
declare str varchar(400);
SET hz_code = ord(substring(s,1,1))-65536 ;
select py into hz_py from pyk where hz_code>=pyk.hz1 and hz_code<=pyk.hz2;
RETURN hz_py;
END$$
delimiter $$
3、先測試一下
mysql> select hzcode('南海龍王');
+--------------------+
| hzcode('南海龍王') |
+--------------------+
| N |
+--------------------+
1 row in set (0.00 sec)
4、建立個測試表
DROP TABLE IF EXISTS `f1`;
create table f1 (
name varchar(30),
pykey varchar(1)
);
insert into f1(name) values
('張三'),
('李四'),
('王五'),
('趙六'),
('錢七');
5、測試
mysql> select * from f1;
+------+-------+
| name | pykey |
+------+-------+
| 張三 | NULL |
| 李四 | NULL |
| 王五 | NULL |
| 趙六 | NULL |
| 錢七 | NULL |
+------+-------+
5 rows in set (0.00 sec)
mysql> update f1 set pykey = hzcode(name);
Query OK, 5 rows affected (0.05 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> select * from f1;
+------+-------+
| name | pykey |
+------+-------+
| 張三 | Z |
| 李四 | L |
| 王五 | W |
| 趙六 | Z |
| 錢七 | Q |
+------+-------+
5 rows in set (0.00 sec)
這樣就很方便地在MYSQL裡查詢漢字的首字母了。 類似地也可以直接在MYSQL得到漢字拼音。 不過需要拼音表,函數寫法也不一樣。