MySQL拼音首字母查詢

2021-10-06 09:21:48 字數 1368 閱讀 5728

最近乙個專案中有模組需要實現拼音首字母查詢功能

實現如下:寫2個函式

第乙個:函式名稱:firstpinyin

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

第二個:pinyin

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, fristpinyin(v_compare));

#set v_return = firstpinyin(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

如何呼叫???

select * from 要查詢的表名   where pinyin(表字段) like '%d%'

例子:select * from tbl_customer tc where pinyin(tc.customer_name) like '%d%'

MySQL拼音首字母查詢

最近乙個專案中有個模組需要實現拼音首字母查詢功能,網上查了一下資料,自己重新修改整理了一下,使其滿足專案的要求。實現過程如下 1.建立乙個獲取中英文大寫首字母函式 html view plain copy drop function if exists get first pinyin char c...

MySQL拼音首字母查詢

在mysql中建立乙個獲取中英文大寫首字母的函式 delimiter create definer function 401 exam get first pinyin char 1 param varchar 255 returns varchar 2 charset utf8 begin dec...

MySql 漢字轉拼音首字母

delimiter use test drop function if exists getpy create definer hjd function getpy in string varchar 21845 returns varchar 21845 charset utf8 begin 擷取...