select * from a where tag like '%北京' and tag like '%三环' and tag like '%XX街'
where charindex('北京',Tag)>0 and charindex('三环',Tag)>0 and charindex('XX街',Tag)>0 这里是用and 还是or 就看你是什么关系了
select * from a where charindex('北京',Tag)>0 and charindex('三环',Tag)>0 and charindex('XX街',Tag)>0
like的效率不是很高的,但实现楼主的具体要求。
多关键字查询 charindex('北京 三环 XX街',Tag)>0
select * from a where charindex('北京',Tag)>0 and charindex('三环',Tag)>0 and charindex('XX街',Tag)>0
--只能这样,先 select * from a where 1=1 --然后根据传入的值按固定分隔符拆分出来,比如你那里的空格为例,拆分后有几个and几个条件 --and charindex('北京',Tag)>0 --and charindex('三环',Tag)>0 --and charindex('XX街',Tag)>0
where charindex(name,'北京 三环 XX街')>0
有多有少在后台做拼接! if(col1!="") { sql=sql+"and tag like '%"+"三环"+"' " }.....有多少拼多少!
declare @a varchar(10) declare @s varchar(1000) declare @t table(name varchar(10)) set @a='a,b,c' set @s=right(replace(','+@a,',',''' as name union select '''),len(replace(','+@a,',',''' as name union select '''))-15)+'''' insert into @t exec(@s) select * from tb b where exists (select 1 from @t a where charindex(a.name,b.name)>0)
select * from a where charindex('北京',Tag)>0 and charindex('三环',Tag)>0 and charindex('XX街',Tag)>0
declare @a varchar(10) declare @s varchar(1000) declare @t table(name varchar(10)) set @a='a,b,c' set @s=right(replace(','+@a,',',''' as name union select '''),len(replace(','+@a,',',''' as name union select '''))-15)+'''' insert into @t exec(@s) select * from tb b where exists (select 1 from @t a where charindex(a.name,b.name)>0)
这里是用and 还是or 就看你是什么关系了
where charindex('北京',Tag)>0 and charindex('三环',Tag)>0 and charindex('XX街',Tag)>0
charindex('北京 三环 XX街',Tag)>0
where charindex('北京',Tag)>0 and charindex('三环',Tag)>0 and charindex('XX街',Tag)>0
--只能这样,先
select * from a where 1=1
--然后根据传入的值按固定分隔符拆分出来,比如你那里的空格为例,拆分后有几个and几个条件
--and charindex('北京',Tag)>0
--and charindex('三环',Tag)>0
--and charindex('XX街',Tag)>0
有多有少在后台做拼接! if(col1!="")
{
sql=sql+"and tag like '%"+"三环"+"' "
}.....有多少拼多少!
declare @s varchar(1000)
declare @t table(name varchar(10))
set @a='a,b,c'
set @s=right(replace(','+@a,',',''' as name union select '''),len(replace(','+@a,',',''' as name union select '''))-15)+''''
insert into @t exec(@s)
select * from tb b where exists
(select 1 from @t a where charindex(a.name,b.name)>0)
where charindex('北京',Tag)>0 and charindex('三环',Tag)>0 and charindex('XX街',Tag)>0
declare @s varchar(1000)
declare @t table(name varchar(10))
set @a='a,b,c'
set @s=right(replace(','+@a,',',''' as name union select '''),len(replace(','+@a,',',''' as name union select '''))-15)+''''
insert into @t exec(@s)
select * from tb b where exists
(select 1 from @t a where charindex(a.name,b.name)>0)