求一个语句:title like '%酒精灯%' or body like '%酒精灯%' 的结果,如何把title like的记录排前面?就是标题包含酒精灯的排在内容含酒精灯的前面

解决方案 »

  1.   

    select 
        *
    from 
        tb
    where 
        title like '%酒精灯%' or body like '%酒精灯%' 
    order by 
        case when title like '%酒精灯%' then 0 else 1 end
      

  2.   


    select * from #temptable where title like '%酒精灯%'
    union
    select * from #temptable where body like '%酒精灯%' 
      

  3.   

    1楼正确。在此我想提醒LZ,你这样写SQL语句做做试验是没有问题的,如果在实际应该中可能会遇到麻烦。当数据量比较大的时候(而且不用很大),估计你这个语句的执行所需要的时间,是你所不能承受的。解决办法:
    用数据库提供的全文索引(full-text index)。如果是MS SQL Server的话,使用中文的全文索引比较简单。如果是Oracle的话,可以参考:
    http://blog.csdn.net/pathuang68/archive/2009/04/20/4093665.aspx如果是MySQL的话,它本身不支持中文的全文索引,但网上有相应的开源包下载以支持中文全文索引的。
      

  4.   

    select 
        *
    from 
        tb
    where 
        title like '%酒精灯%' or body like '%酒精灯%' 
    order by 
        case when title like '%酒精灯%' then 0 else 1 end快一点