我现在把客户端查询条件分割到一个表。类似这样
Id Key (tb)
1 张三
2 13809652318
3 北京
4 服装 查询时我把主表需要的字串连接起来
CusName+' ' + Tel as CusInfo
City +' ' +Cargo as CargoInfo
在where 条件中我是这样写的,
where (exists(select id from tb where charindex(key,CusName)>0) or 1=1)
and exists(select id from tb where charindex(key,CusName)>0) or 1=1)
这样能查询出来数据,但是当上面条件满足了,下面条件不满足的时候就返回了所有行了。
像大虾求教了!另求大虾能给个更好的方式来做这样的查询。
Id Key (tb)
1 张三
2 13809652318
3 北京
4 服装 查询时我把主表需要的字串连接起来
CusName+' ' + Tel as CusInfo
City +' ' +Cargo as CargoInfo
在where 条件中我是这样写的,
where (exists(select id from tb where charindex(key,CusName)>0) or 1=1)
and exists(select id from tb where charindex(key,CusName)>0) or 1=1)
这样能查询出来数据,但是当上面条件满足了,下面条件不满足的时候就返回了所有行了。
像大虾求教了!另求大虾能给个更好的方式来做这样的查询。
而且,不妨把主表、你的查询语句都拿出来。
if object_id('[tb]') is not null drop table [tb]
create table [tb]([ID] int,[Name] varchar(200))
go
insert [tb]
--select 1,'蓝天'
--union all
select 1,'蓝天 计算机'
declare @split nvarchar(2)
set @split=' '
select distinct t.ID,Name=substring(t.Name,r.number,charindex(@split,t.Name+@split,r.number)-r.number)
into #temp
from tb t,master..spt_values r
where r.number<=len(t.Name) and type='P'
and substring(@split+t.Name,r.number,1)=@split
select * from tb
drop table tb
我现在就需要根据tb这个表的数据来查询过滤数据了。
select * from tb where 1=1 and name like '%计算机%'and name like '%蓝天%'
ID Key
1 蓝天
2 计算机主板数据是这样的,信息字段是我根据租户信息组合起来的。
单号(Order) 信息(Info)
011117 蓝天 张小姐 13899999999 计算机
011118 顺天 王生 13099999999 办公桌我想根据上面的关键词临时表中的数据,在主表中递归查询唯一的信息。
现在我的解决方法是根据关键词来拼接成下面的sql
select * from table where 1=1 and charindex('蓝天',info)>0
and charindex('计算机',info)>0
求大虾能给个高效些的方案。