当前位置: 代码迷 >> PB >> 请教大家通过什么方法能够实现首字母检索
  详细解决方案

请教大家通过什么方法能够实现首字母检索

热度:161   发布时间:2016-04-29 09:23:54.0
请问大家通过什么方法能够实现首字母检索
比如我找“三国演义”, 输入搜索栏里输入“sgyy”,就能找到所有以sgyy音序开头的条目了,请问各位大侠们在PB里如何实现?

------解决方案--------------------
C/C++ code
$PBExportHeader$f_get_py.srf$PBExportComments$取首位汉字的拼音的函数global type f_get_py from function_objectend typeforward prototypesglobal function string f_get_py (string as_data)end prototypesglobal function string f_get_py (string as_data);long ll_pos string ls_py,ls_pos FOR ll_pos = 1 TO Len(as_data)  IF ASC(Mid(as_data,ll_pos,1)) > 128 THEN     CHOOSE CASE Mid(as_data,ll_pos,2)        CASE is >= '匝';ls_py += "Z"        CASE is >= '丫';ls_py += "Y"        CASE is >= '夕';ls_py += "X"        CASE is >= '哇';ls_py += "W"        CASE is >= '他';ls_py += "T"        CASE is >= '撒';ls_py += "S"        CASE is >= '然';ls_py += "R"        CASE is >= '七';ls_py += "Q"        CASE is >= '趴';ls_py += "P"        CASE is >= '哦';ls_py += "O"        CASE is >= '拿';ls_py += "N"        CASE is >= '妈';ls_py += "M"        CASE is >= '廓';ls_py += "L"        CASE is >= '咖';ls_py += "K"        CASE is >= '讥';ls_py += "J"        CASE is >= '哈';ls_py += "H"        CASE is >= '嘎';ls_py += "G"        CASE is >= '发';ls_py += "F"        CASE is >= '讹';ls_py += "E"        CASE is >= '搭';ls_py += "D"        CASE is >= '擦';ls_py += "C"        CASE is >= '八';ls_py += "B"        CASE is >= '阿';ls_py += "A"        CASE ELSE;      ls_py += Mid(as_data,ll_pos,2)     END CHOOSE     ll_pos ++  ELSE     ls_py += Mid(as_data,ll_pos,1)  END IF NEXT RETURN ls_pyend function================================================================================$PBExportHeader$f_get_allpy.srf$PBExportComments$获得多个汉字字符串的拼音global type f_get_allpy from function_objectend typeforward prototypesglobal function string f_get_allpy (string as_str)end prototypesglobal function string f_get_allpy (string as_str);string ls_py,ls_temp,ls_test,ls_singleinteger li_i,ll_j,li_lenthli_lenth = len(as_str)ll_j = 1  ls_py = ""for li_i = 1 to li_lenth    ls_temp = mid(as_str,li_i,1)    if asc(ls_temp)>128 then        ls_test = mid(as_str,ll_j,2)        ll_j = ll_j + 2    else        ls_test = mid(as_str,ll_j,1)        ll_j = ll_j + 1    end if        ls_single = lower(f_get_py(ls_test))    ls_py = ls_py + ls_single nextreturn ls_pyend function
------解决方案--------------------
建议写个字段来保留首字母,检索的时候至少会快很多。
------解决方案--------------------
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



--创建取拼音函数
ALTER function fGetPy(@Str varchar(500)='')
returns varchar(500)
as
begin
declare @strlen int,@return varchar(500),@ii int
declare @n int,@c char(1),@chn nchar(1)

select @strlen=len(@str),@return='',@ii=0
set @ii=0
while @ii<@strlen
begin
select @[email protected]+1,@n=63,@chn=substring(@str,@ii,1)
if @chn>'z'
select @n = @n +1
,@c = case chn when @chn then char(@n) else @c end
from(
select top 27 * from (
select chn = '吖'
union all select '八'
union all select '嚓'
union all select '咑'
union all select '妸'
union all select '发'
union all select '旮'
union all select '铪'
union all select '丌' --because have no 'i'
union all select '丌'
union all select '咔'
union all select '垃'
union all select '嘸'
union all select '拏'
union all select '噢'
union all select '妑'
union all select '七'
union all select '呥'
union all select '仨'
union all select '他'
  相关解决方案