我现在把客户端查询条件分割到一个表。类似这样
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)
这样能查询出来数据,但是当上面条件满足了,下面条件不满足的时候就返回了所有行了。
像大虾求教了!另求大虾能给个更好的方式来做这样的查询。

解决方案 »

  1.   

    你Where子句的条件好像跟And条件的语句是一样的,照你的查询,应该无论如何都会返回所有行吧。
    而且,不妨把主表、你的查询语句都拿出来。
      

  2.   

    是这样的。客户在界面输入他想查询的关键词,比如客户想查 蓝天公司租赁的 计算机,他就输入 "蓝天 计算机" ,然后我在SQL里把 “蓝天 计算机”分割成一个表的两条数据 。分割语句:
    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这个表的数据来查询过滤数据了。
      

  3.   

    select 'and name like ''%'+Name+'%''' from #temp FOR XML PATH('')
    select * from tb where 1=1 and name like '%计算机%'and name like '%蓝天%'
      

  4.   

    客户输入的查询关键词“蓝天 计算机"在sql中分割成一个查件关键词的临时表
    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
    求大虾能给个高效些的方案。