有一个表结构! 文章标题      文章内容 
============ ==================  
标题一        相关内容 
标题二        相关内容 
...            ... 
============ ================== 写一条查询语句,根据给定的关键字进行筛选,选出文章标题或内容中包含关键字的记录。关键是要对于查询结果进行排序,如果关键字出现在文章标题中排在前面,如果关键字出现在文章内容中则排在后面。如果基础现在标题中又出现在内容中出现在最后。 哥们新手,没什么分,各位高手见谅!

解决方案 »

  1.   

    union all?要用like,又要全表扫描,,效率可能不咋的
      

  2.   

    select * from table1
    where instr(标题,'xxx')>0
      or instr(内容,'xxx')>0
    order by case when instr(标题,'xxx')>0 then 1 else 2 end
      

  3.   

    SELECT 文章标题,文章内容 FROM tb 
    WHERE 文章标题 LIKE '%关键字%' OR 文章内容 LIKE '%关键字%'
    ORDER BY CASE WHEN 文章标题 LIKE '%关键字%' AND 文章内容 NOT LIKE '%关键字%' THEN 1
    WHEN 文章标题 NOT LIKE '%关键字%' AND 文章内容 LIKE '%关键字%' THEN 2
    WHEN 文章标题 LIKE '%关键字%' AND 文章内容 LIKE '%关键字%' THEN 3
    ELSE 4 END;
      

  4.   

    select * from article a where a.title like '%企鹅%' and not a.content like '%企鹅%'
    union all
    select * from article a where not a.title like '%企鹅%' and  a.content like '%企鹅%'
    union all
    select * from article a where a.title like '%企鹅%' and  a.content like '%企鹅%'
      

  5.   

    没看清楚要求
    order by case when 标题 like '%XXX%' and 内容 like '%XXX%' then 3
      when 标题 like '%XXX%' then 1
      else 2 end;性能应该更好一些