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 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?