今日面试遇到的一道题,大家来看看

firegod2005 2010-06-25 09:40:43
有个表pages ,有5个属性,id, url, title, body, site.
问: 用一条SQL语句搜出url,body,或site中包含’test’字符串的数据,且将含有‘test’的url 的结果放在最前面,其次是body, 然后是site 且不能有重复数据
如:
id url title body site
1 'a' 'b' 'c' 'd'
2 'a' 'b' 'c' 'test'
3 'a' 'b' 'test' 'd'
4 'ftest' 'b' 'c' 'd'
5 'test' 'b' 'test' 'test'
搜出的结果是:
id url title body site
4 'ftest' 'b' 'c' 'd'
5 'test' 'b' 'test' 'test'
3 'a' 'b' 'test' 'd'
2 'a' 'b' 'c' 'test'

...全文
693 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
CqCoder 2010-06-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 gdjlc 的回复:]
已测试过:


SQL code

select * from pages where url like '%test%' or body like '%test%' or site like '%test%'
order by case
when url like '%test%' then 0
when body like '%test%' then 1
when si……
[/Quote]


经测试 这个最好! 牛淫!!!!!!!
未来在这儿 2010-06-30
  • 打赏
  • 举报
回复
distinct?
老头 2010-06-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 gdjlc 的回复:]

已测试过:

SQL code

select * from pages where url like '%test%' or body like '%test%' or site like '%test%'
order by case
when url like '%test%' then 0
when body like '%test%' then 1
when site lik……
[/Quote]

恩 我写的跟你的差不多 不过你是如何去掉 重复数据的。
badmonkey1 2010-06-30
  • 打赏
  • 举报
回复
学习学习
S314324153 2010-06-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 gdjlc 的回复:]
已测试过:


SQL code

select * from pages where url like '%test%' or body like '%test%' or site like '%test%'
order by case
when url like '%test%' then 0
when body like '%test%' then 1
when si……
[/Quote]

这个效率应该比union好
gdjlc 2010-06-27
  • 打赏
  • 举报
回复
已测试过:


select * from pages where url like '%test%' or body like '%test%' or site like '%test%'
order by case
when url like '%test%' then 0
when body like '%test%' then 1
when site like '%test%' then 2 end
fondOfJava 2010-06-26
  • 打赏
  • 举报
回复
强烈 建议 你 加分。
fondOfJava 2010-06-26
  • 打赏
  • 举报
回复


select p.id,p.url,p.title,p.body,p.site from pages p
where p.url like '%test%'
or p.body like '%test%'
or p.site like '%test%'
order by length(p.url) desc
;
fly_Apple10 2010-06-26
  • 打赏
  • 举报
回复
,,我也想苗苗这个sql不过有人写了就帮顶一下
楼主太小气了
z55396496 2010-06-26
  • 打赏
  • 举报
回复
路过学习
luckyzqy 2010-06-26
  • 打赏
  • 举报
回复
学习学习,路过
firegod2005 2010-06-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 mailpq 的回复:]
SQL code
(select id,url,title,body,site from pages where url like '%test%') union all
(select id,url,title,body,site from pages where url not like '%test%' and body like '%test%') union all (select ……
[/Quote]
完全正确,不过是不是太长了点?
firegod2005 2010-06-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fondofjava 的回复:]
SQL code


select p.id,p.url,p.title,p.body,p.site from pages p
where p.url like '%test%'
or p.body like '%test%'
or p.site like '%test%'
order by length(p.url) desc
;
[/Quote]
不好意思,回答错误
倘若我有这样一行数据
id url title body site
6 'fffffffffff' 'testb' 'testc' 'd'
按照你的方法,那么这句会被排在最前面,显然不符合要求
mailpq 2010-06-26
  • 打赏
  • 举报
回复
(select id,url,title,body,site from pages where url like '%test%') union all
(select id,url,title,body,site from pages where url not like '%test%' and body like '%test%') union all (select id,url,title,body,site from pages where url not like '%test%' and body not like '%test%' and site like '%test%')

不过这个效率...
shenyangwozuishuai 2010-06-26
  • 打赏
  • 举报
回复
强烈 建议 你 加分。
神之泪花 2010-06-25
  • 打赏
  • 举报
回复
SF,睡觉前帮顶,

594

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧