estpzit 2020-12-07 10:37 采纳率: 80%
浏览 7
已采纳

SQL 50 题 第九题

原文 https://blog.csdn.net/qq_41080850/article/details/84648897 最后

 select * from student where sid in
    (select sid from
    (select sid,cid from grade where sid in
    (select sid from grade where sid !='06' group by sid
    having count(*)=(select count(*) from grade where sid='06' group by sid))) as t1
    inner join (select cid from grade where sid='06') as t2
    on t1.cid = t2.cid
    group by t1.sid 
    having count(*)=(select count(*) from grade where sid='06'));

这是原文最后的另一种求法,最后一句 为什么不可以改成 having count(cid)=(select count(*) from grade where sid='06'));

其他的都可以将 (*)换成(cid)

另外 用 on t1.cid = t2.cid 的原因是因为固定课程吗?这里用cid相同有点懂又有点不懂。

  • 写回答

3条回答 默认 最新

  • sinJack 2020-12-07 11:20
    关注

    不是不可以。其实是可以的。只是你改成having count(cid)=(select count(*) from grade where sid='06'))会报错:

    Column 'cid' in having clause is ambiguous意思是存在多个相同的字段名。

    你需要指明某一个表中的字段名就好了。指定t1或者t2的。

     having count(t1.cid)=(select count(*) from grade where sid='06'));

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包