SQL多关键字模糊查询
2013-04-03 17:00
471 查看
在项目当中,通常会用到用空格分隔的多个关键字进行查询,博主为此留下了此方法
if object_id('[tb]') is not null
drop table [tb]
create table [tb] (id int,name varchar(25))
insert into [tb]
select 1,'asdf富士康sfsaf'
union all select 2,'富士康sdaf跳楼'
union all select 3,'sdaf跳楼'
union all select 4,'asdf富士康as跳楼郭台铭asf'
union all select 5,'郭台铭asf'
declare @name varchar(50),
@sql varchar(8000)
set @Name='富士康 跳楼 郭台铭'
set @sql=' (case when charindex('''+replace(@name,' ',''',name)>0 then 1 else 0 end)+(case when charindex(''')+''',name)>0 then 1 else 0 end)'
set @sql='select * from tb order by '+@sql+' desc'exec(@sql)
=================================SQL多关键字并操作=================================================
declare @name varchar(50),
@sql varchar(8000)
set @Name='大三男生 千米跑后 学校 继续'
set @sql=' charindex('''+replace(@name,' ',''',T1.Title)>0 and charindex(''')+''',T1.Title)>0'
set @sql='select * from DebriefInfo T1 where '+@sql exec(@sql)
if object_id('[tb]') is not null
drop table [tb]
create table [tb] (id int,name varchar(25))
insert into [tb]
select 1,'asdf富士康sfsaf'
union all select 2,'富士康sdaf跳楼'
union all select 3,'sdaf跳楼'
union all select 4,'asdf富士康as跳楼郭台铭asf'
union all select 5,'郭台铭asf'
declare @name varchar(50),
@sql varchar(8000)
set @Name='富士康 跳楼 郭台铭'
set @sql=' (case when charindex('''+replace(@name,' ',''',name)>0 then 1 else 0 end)+(case when charindex(''')+''',name)>0 then 1 else 0 end)'
set @sql='select * from tb order by '+@sql+' desc'exec(@sql)
=================================SQL多关键字并操作=================================================
declare @name varchar(50),
@sql varchar(8000)
set @Name='大三男生 千米跑后 学校 继续'
set @sql=' charindex('''+replace(@name,' ',''',T1.Title)>0 and charindex(''')+''',T1.Title)>0'
set @sql='select * from DebriefInfo T1 where '+@sql exec(@sql)
相关文章推荐
- SQL 语句中模糊搜索查询 LIKE 关键字
- SQL“多字段模糊匹配关键字查询”[转载]
- sql模糊查询查询多个关键字
- SQL 多字段模糊匹配关键字查询
- SQL模糊查询,一个关键字查询多个字段的方法
- SQL中LIKE模糊查询LIKE关键字匹配语句
- 【ZT】SQL实现多字段模糊匹配关键字查询
- 关于Sql语句中的模糊查询like关键字详解
- oracle sql语言模糊查询--通配符like
- 关于SQL时间类型的模糊查询 .
- mybatis的动态SQL与模糊查询
- sql模糊查询
- sql concat like 多字段模糊查询 其中有null
- SQL 模糊查询
- oracle Like模糊查询与带有关键字Reverse的索引应用
- SQL like模糊查询
- 【资料】sql模糊查询
- 有关SQL模糊查询
- mybitis 用不同数据库模糊查询的 sql 语句的写法
- SQL的模糊查询—存储过程