场景:
需要通过用户输入的姓名关键字来搜索用户。用户输入关键字'x'来搜索用户(数据来源于表[Name字段中]或内存[List<UserInfo>]中)
要求:
得到的结果排序应为:
x
xia
xiao
yx
即:
各位大侠能否给出一套c#与sql server(2008)的解决方案?
补充:
如果能一起解决中文问题最好,如搜索'x'
得到的结果排序应为:
x
xiani
夏荣
肖小笑
杨星
即将汉字的拼音首字母纳入在内,不知sqlserver是否支持这一特性的搜索?
在中等数据库的情况下要保证效率的话 建议冗余一个字段 一个记录首字的拼音[FirstChar]
select * from table where FirstChar='x' or ( FirstChar='x' and Name like '%x%')
order by ( )...
order by 的逻辑是 如果是FirstChar='x' 那么给length(Name) -1000 否则 返回 length(Name)
然后从低到高排
这样代码比较简单 效率也还可以
当然如果是大数据量(>=1000000 records) 那要做的事情就多了
PS: 把长度也冗余出来有助于提高查询性能
PS:如果有分页的话 说不定分开查找性能更好 (前提是首字母命中率足够高)
PS:分表也可以 如果压力太大了 按照首字母分表
由于时间缘故没做过多解释
有什么问题及时提出
---------------------准备工作 开始-------------------------------
if object_id('zhuisuos')is not null
drop table zhuisuos
go
create table zhuisuos
(
name varchar(100)
)
insert into zhuisuos values('xia')
insert into zhuisuos values('dxc')
insert into zhuisuos values('x')
insert into zhuisuos values('xx')
insert into zhuisuos values('xiani')
insert into zhuisuos values('yx')
insert into zhuisuos values('夏荣')
insert into zhuisuos values('肖小笑')
insert into zhuisuos values('杨星')
go
--建立函数
if object_id('fn_getfirstspell')is not null
drop function fn_getfirstspell
go
GO
create function [dbo].fn_getfirstspell
(@str nvarchar(4000))
returns nvarchar(4000)
as
begin
declare @str_len int,@result nvarchar(4000)
declare @zhuisuo table
(firstspell nchar(1) collate Chinese_PRC_CI_AS,
letter nchar(1))
set @str_len=len(@str)
set @result= ' '
insert into @zhuisuo
(firstspell,letter)
select '吖 ', 'A ' union all select '八 ', 'B ' union all
select '嚓 ', 'C ' union all select '咑 ', 'D ' union all
select '妸 ', 'E ' union all select '发 ', 'F ' union all
select '旮 ', 'G ' union all select '铪 ', 'H ' union all
select '丌 ', 'J ' union all select '咔 ', 'K ' union all
select '垃 ', 'L ' union all select '嘸 ', 'M ' union all
select '拏 ', 'N ' union all select '噢 ', 'O ' union all
select '妑 ', 'P ' union all select '七 ', 'Q ' union all
select '呥 ', 'R ' union all select '仨 ', 'S ' union all
select '他 ', 'T ' union all select '屲 ', 'W ' union all
select '夕 ', 'X ' union all select '丫 ', 'Y ' union all
select '帀 ', 'Z '
while @str_len> 0
begin
select top 1 @result=letter+@result,@str_len=@str_len-1
from @zhuisuo
where firstspell <=substring(@str,@str_len,1)
order by firstspell desc
if @@rowcount=0
select @result=substring(@str,@str_len,1)+@result,@str_len=@str_len-1
end
return(@result)
end
---------------------准备工作 结束-------------------------------
--正式查询
declare @str varchar(10)
set @str='x'
create table #result
(name varchar(100) null,id int null,lens int null)
insert into #result
select name,1,len(name) from zhuisuos
where name like @str+'%'
insert into #result
select name,2,len(name) from zhuisuos
where name like '%'+@str+'%' and name not like @str+'%'
insert into #result
select name,3,len(name) from zhuisuos
where dbo.fn_getfirstspell (name) like @str+'%' and name not like @str+'%' and name not like '%'+@str+'%'
insert into #result
select name,4,len(name) from zhuisuos
where dbo.fn_getfirstspell (name) like '%'+@str+'%' and dbo.fn_getfirstspell (name) not like @str+'%'
and name not like @str+'%' and name not like '%'+@str+'%'
select name from #result
order by id,lens
drop table #result
可以通过linq高模糊查询,简单方便!