读书人

SQL

发布时间: 2012-02-13 17:20:26 作者: rapoo

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 

读书人网 >J2EE开发

热点推荐