日期:2014-05-20  浏览次数:20727 次

SQL求助
sql小测试:有张表pages 列:id , url , content , body  
类型:id --int 后面3个varchar
现在要求查询:url 中包含google字符串的显示在最上面,body的显示在中间,content的显示在最下面,请使用一条语句查询!


------解决方案--------------------
SQL code
select * from pages where id in ( -- get all records
   select distinct id from pages where id in ( --remove duplicate records
     select id from pages where url like '%google%' --url contains 'google'
     union all 
     select id from pages where content like '%google%'  --content contains 'google'
     union all 
     select id from pages where body like '%google%'  --body contains 'google'
   )
)

------解决方案--------------------
SQL code

select url,body,content
       decode(instr(url, 'google'),
              0,
              decode(instr(body, 'google'),
                     0,
                     decode(instr(content, 'google'), 0, 4, 3),
                     2),
              1) type_order
  from pages 
 order by type_order