level 1
最好用上not exists语句,虽然我知道别的方法一就可以找出来。还是拜托大神了。
2013年04月15日 13点04分
1
level 2
你的意思是1号和2号任一门把,那就是下面了
select sno from grade where cno='01' or cno='02'
2013年04月15日 14点04分
4
你理解错了,是至少选修了1和2,就是必须包括1和2.
2013年04月15日 14点04分
level 2
select sno from grade where cno='01' or cno='02' group by sno having count(sno)=2
2013年04月15日 14点04分
5
level 1
那你能帮我看看我写的为什么就没结果呢。
select distinct sno from grade g1
where not exists(select sno from grade g2
where g2.cno='01' and not exists
(select sno from grade g3 where g3.cno='02'
and g3.cno=g1.cno and g3.cno=g2.cno));
2013年04月15日 15点04分
6
语句是你写的吗?感觉逻辑不对 不符合你的要求
2013年04月15日 15点04分
回复 sql狂人 :逻辑完全混乱,明显错的
2013年04月15日 15点04分
这语句的思路我没有理解错的话就是,将结果去掉那些只选了01但是没有选02的学生;跟你的题意不符;还有一点就是为什么要将最后连接写在最后,我没有这样用过
2013年04月15日 15点04分
回复 灌水四人组之蛋 :今天课上刚学了,理解还不是那么透彻,自己摸索着写的,恳请大神指点迷津。
2013年04月15日 15点04分
level 2
如果硬要用not exists 我想到的就比较烦了
首先找到1,和2 都不选的学号
select sno from grade a where cno not in ('01','02') and not exists (select sno from grade b where a.sno=b.sno cno in ('01','02'))
然后再过滤掉1,和2 都不选的学号
最终就是
select sno from grade where cno not in (select sno from grade a where cno not in ('01','02') and not exists (select sno from grade b where a.sno=b.sno cno in ('01','02')))
2013年04月15日 15点04分
7
期间你可以将所有in 和not in 都用 exists 和not esists 替代
2013年04月15日 15点04分
回复 sql狂人 :你的思路我倒是理解了,刚才在sql2008上试了下,查询结果不对啊,这次多吃来几个人,对于这个问题,可能是我考虑的复杂了吧,不过我还是想搞懂exists语句查询,还有就是相关性查询。这些都是今天课上老师讲的,有的地方听得迷迷糊糊的。
2013年04月15日 15点04分
抱歉,打了个错别字,应该是“这次多出来几个人”。
2013年04月15日 15点04分
回复 我心清净2 :
![[汗]](/static/emoticons/u6c57.png)
突然细想了一下,我思路也是错了,
2013年04月15日 15点04分
level 2
换个思路:找出选了只选01 或只选了02 ,或者选了01和02的
select sno from grade where cno='01' or cno='02'
找出 只选了01,或者只选了02 的记录
select sno from grade where cno='01' or cno='02' group by sno having count(sno)=1
然后去掉只选了01,或者只选了02 的记录
最终就是
select sno from grade b where cno='01' or cno='02' and not esists (select sno from grade a where b.sno=a.sno and cno='01' or cno='02' group by sno having count(sno)=1
)
2013年04月15日 15点04分
8
level 2
哈哈,我记得我当初听课,其实最难理解就是esists 其他都很简单;就是现在我的理解也就是
esists 用于可以多条件匹配, 而 in 只能单个条件匹配;就像你现在这个表,你只需要匹配sno就行了,所以完全可以用in 和not in 完成;
2013年04月15日 15点04分
9
level 2
其实当你能够灵活运用sql语句的时候,真正难的不是编写语句;而是你解决问题的思路
越是复杂的问题,越是需要一步步解决,如果可以一下子想通解决的,那只是说明问题不够复杂;我接触的好几种语言感觉最简单的就是sql,语句的编写很适合人的阅读
2013年04月15日 15点04分
10
用集合的思想来说,去掉1、2都不选的、只选1和只选2的就行了
2013年04月15日 15点04分
现在基本热门的都是高级语言了,想当年学机器码和汇编只是痛苦啊
2013年04月15日 15点04分
回复 灌水四人组之蛋 :嗯,我第一次只是去掉了1、2都不选的;如果还要加上只选1和只选2 那样效率就太低了,所以换了个思路
2013年04月15日 16点04分
回复 灌水四人组之蛋 :
![[汗]](/static/emoticons/u6c57.png)
没接触过那么底层的,大哥多大年纪了
2013年04月15日 16点04分
level 1
select distinct sno from grade g1where sno in(select sno from grade g1 where g1.cno='01' and not exists (select * from grade g2 where g2.cno='02' and not exists (select * from grade g3 where g3.sno=g1.sno and g2.cno=g3.cno)));
2013年04月16日 01点04分
11
level 1
我自己试过
正确的
select sname
from student
where not exists(select sno from sc where cno=1 and cno=2)
2017年12月28日 02点12分
14
cno可以等于1又等于2吗
![[呵呵]](/static/emoticons/u5475u5475.png)
?
2023年03月27日 02点03分
level 5
标准答案:
select sno
from grade
WHERE cno IN ('01','02')
GROUP BY sno
HAVING COUNT(1)<>1
2017年12月28日 03点12分
15