作者rockchangnew (rock)
看板Database
标题Re: [SQL ] 资料选取问题
时间Fri Mar 11 23:18:20 2016
※ 引述《littlepigred (小猪鸿)》之铭言:
: 资料库名称:MySQL
: 资料库版本:
: 内容/问题描述:
: 今天去面试遇到一题题目如下:
: name subject score
: ────────────
: aaa math 90
: aaa chem 75
: aaa eng 70
: bbb chem 85
: bbb math 95
: bbb eng 90
: ccc eng 65
: ccc chem 80
: ccc math 75
: 请用SQL语法选出至少2科分数高於85且没有任何一科低於75的人
: 小弟新手 麻烦各为求解QQ
select name from(
select
name
,count(case when score>84 then 1 else null end) as [Over85]
,count(case when score<75 then 1 else null end) as [Below75]
from scoretb group by name) tmp
where [Over85]>=2 and [Below75]=0
--
Sent from my Windows
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.34.194.8
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1457709503.A.735.html
1F:→ rockchangnew: 我是用MSSQL,MySQL您要试试看 03/12 00:42
2F:推 mrforget: select name from tab where score >= 85 and exists 03/12 01:31
3F:→ mrforget: (select name from tab where score <=75) 03/12 01:32
4F:→ mrforget: group by name having count(name)>=2; 03/12 01:32
5F:→ mrforget: 我是用oracle 03/12 01:32
6F:→ mrforget: <=75那边有错,应该是<75 03/12 01:33
7F:推 littlepigred: 我是原po 谢谢各位的解答 考试只说用SQL语法 没特 03/12 23:51
8F:→ littlepigred: 别设限 03/12 23:51
9F:→ littlepigred: 不过我用MYSQL 好像不能用exists执行 我再研究 不过 03/12 23:52
10F:→ littlepigred: 概念我了解了 谢谢各位! 03/12 23:52