select ID ,KELX from table1 where KELX like '%网站%' and KELX like '%域名%' and KELX like '%实名%'这个样的话"网站,域名,实名,邮箱"也就被搜出来了
select ID ,KELX from table1 where left(KELX,2) like '%网站%' and len(KELX)>5
select ID ,KELX from table1 where KELX like '%网站%' and KELX like '%域名%' and KELX like '%实名%' and len(KELX) = len('网站,域名,实名')
select ID ,KELX from table1 where left(KELX,2) like '%网站%' and len(KELX)>5更让人不懂了 我是在WEB里选择复选框,选择哪几项,就精确查询出哪几项业务类型的客户
--写个自定义函数处理就行了if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_compstrlist]') and xtype in (N'FN', N'IF', N'TF')) drop function [dbo].[f_compstrlist] GOif exists (select * from dbo.sysobjects where id = object_id(N'[序数表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [序数表] GO--为了效率,所以要一个辅助表配合 select top 4000 id=identity(int,1,1) into 序数表 from syscolumns a,syscolumns b alter table 序数表 add constraint pk_id_序数表 primary key(id) go/*--字符串比较函数 比较两个字符串列表是否相同,顺序可以不同--邹建 2004.12(引用请保留此信息)--*//*--调用示例 select dbo.f_compstrlist('实名,网站,域名','网站,域名,实名',',') --*/ create function f_compstrlist( @str1 nvarchar(4000), --要比较的字符串1 @str2 nvarchar(4000), --要比较的字符串2 @splitchar nvarchar(10) --字符串列表的分隔符 )returns int as begin if len(@str1)<>len(@str2) return(0) if len(@str1)-len(replace(@str1,@splitchar,'')) <>len(@str2)-len(replace(@str2,@splitchar,'')) return(0) declare @r bit declare @t table(col nvarchar(1000)) insert @t select @splitchar+substring(@str1,id,charindex(@splitchar,@str1+@splitchar,id)-id)+@splitchar from 序数表 where id<=len(@str1) and charindex(@splitchar,@splitchar+@str1,id)-id=0 set @r=case when @@rowcount=(select count(*) from @t where charindex(col,@splitchar+@str2+@splitchar)>0) then 1 else 0 end return(@r) end go
--调用上面的函数实现楼主的处理要求--示例表 create table table1(ID int,KELX nvarchar(100)) insert table1 select 1,'网站,实名,域名' union all select 2,'网站,实名' union all select 3,'网站,域名,实名' union all select 4,'邮箱,搜索,空间' union all select 5,'实名,域名,邮箱' union all select 6,'实名' go--调用函数进行查询 select * from table1 where dbo.f_compstrlist(KELX,'网站,域名,实名',',')=1 go--删除测试 drop table table1/*--测试结果ID KELX ----------- ------------------- 1 网站,实名,域名 3 网站,域名,实名(所影响的行数为 2 行) --*/
我是在WEB里选择复选框,选择哪几项,就精确查询出哪几项业务类型的客户
drop function [dbo].[f_compstrlist]
GOif exists (select * from dbo.sysobjects where id = object_id(N'[序数表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [序数表]
GO--为了效率,所以要一个辅助表配合
select top 4000 id=identity(int,1,1) into 序数表
from syscolumns a,syscolumns b
alter table 序数表 add constraint pk_id_序数表 primary key(id)
go/*--字符串比较函数 比较两个字符串列表是否相同,顺序可以不同--邹建 2004.12(引用请保留此信息)--*//*--调用示例 select dbo.f_compstrlist('实名,网站,域名','网站,域名,实名',',')
--*/
create function f_compstrlist(
@str1 nvarchar(4000), --要比较的字符串1
@str2 nvarchar(4000), --要比较的字符串2
@splitchar nvarchar(10) --字符串列表的分隔符
)returns int
as
begin
if len(@str1)<>len(@str2) return(0)
if len(@str1)-len(replace(@str1,@splitchar,''))
<>len(@str2)-len(replace(@str2,@splitchar,'')) return(0)
declare @r bit
declare @t table(col nvarchar(1000))
insert @t select @splitchar+substring(@str1,id,charindex(@splitchar,@str1+@splitchar,id)-id)+@splitchar
from 序数表
where id<=len(@str1) and charindex(@splitchar,@splitchar+@str1,id)-id=0
set @r=case when @@rowcount=(select count(*) from @t where charindex(col,@splitchar+@str2+@splitchar)>0)
then 1 else 0 end
return(@r)
end
go
create table table1(ID int,KELX nvarchar(100))
insert table1 select 1,'网站,实名,域名'
union all select 2,'网站,实名'
union all select 3,'网站,域名,实名'
union all select 4,'邮箱,搜索,空间'
union all select 5,'实名,域名,邮箱'
union all select 6,'实名'
go--调用函数进行查询
select * from table1 where dbo.f_compstrlist(KELX,'网站,域名,实名',',')=1
go--删除测试
drop table table1/*--测试结果ID KELX
----------- -------------------
1 网站,实名,域名
3 网站,域名,实名(所影响的行数为 2 行)
--*/