需求描述,输入关键字:资 料
注意资料中间是有空格的,想模仿搜索引擎,首先查询强组合:(资料) ,然后查询(资 and 料),最后查询(资 or 料)
但是这样查询出来的有重复的,下面sql中id是主键--关键字:资料 -207
select t.tm,t.id from table t where t.tm like '%资料%'
union all
--关键字:资 and 料 -209
select t.tm,t.id from table t where t.tm like '%资%' and t.tm like '%料%'
union all
--关键字:资 or 料 -321
select t.tm,t.id from table t where t.tm like '%资%' or t.tm like '%料%'求一个简单高效的sql语句。

解决方案 »

  1.   

    如果有重复项直接在前面加上distinct,如:
    select distinct t.id,t.tm from table t where t.tm like '%资料%';
      

  2.   

    用union,不用union all就不会重复了,不过union all速度快些
      

  3.   


    --速度还是很慢,只是写法简单点
    select 
    t.tm,
    (case when instr(t.tm,'资料')>0 then -207 
    when instr(t.tm,'资')>0 and instr(t.tm,'料')>0 then -209
    else -321 end) code,
    t.id 
    from "table" t where t.tm like '%资%' or t.tm like '%料%';
      

  4.   

    这一句就能得出3个合并的结果了
    select t.tm,t.id from table t where t.tm like '%资%' or t.tm like '%料%'
      

  5.   

    试试这种with t as (
    select '资料' as fname from dual
    union all
    select 'aa资料bb' from dual
    union all
    select 'aa资 料bb' from dual
    union all 
    select 'aa资c 料bb' from dual
    union all
    select 'aa资cd料bb' from dual
    union all 
    select 'aaccbb' from dual
    )
    select * from t
    where regexp_like(fname,'资[[:alnum:]]*|[[:space:]]料')
    --[[:alnum:]]*:任意字母或者数字0次或多次
    --[[:space:]]任意白字符
    FNAME      
    ---------- 
    资料       
    aa资料bb   
    aa资 料bb  
    aa资c 料bb 
    aa资cd料bb