请问子查询中不能使用上一层的临时表为什么?

KetchupZ 2019-05-21 11:04:48
查询选课表sc中出现被选课程最多的课程的信息
sc表:(Sid,Cid,Score)
course表 :(Cid,Cname,Tid)

--查询选课表sc中出现被选课程最多的课程的信息--这是一个错误的代码
select course.*
from course,(
select cid,count(*) as cnt
from sc
group by cid )as t1 --t1是cid 和对应次数的表
where t1.cnt=(select max(t1.cnt) from t1) AND course.cid=t1.cid

图片
...全文
130 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
KetchupZ 2019-05-21
  • 打赏
  • 举报
回复
引用 1 楼 卖水果的net 的回复:
-- 定义成一个 with 块 with t1 as (     select cid,count(*) as cnt      from sc      group by cid  ) select course.* from course,t1 --t1是cid 和对应次数的表 where t1.cnt=(select max(t1.cnt) from t1) AND course.cid=t1.cid
谢谢解答,涨知识了!
卖水果的net 2019-05-21
  • 打赏
  • 举报
回复
-- 定义成一个 with 块 with t1 as (     select cid,count(*) as cnt      from sc      group by cid  ) select course.* from course,t1 --t1是cid 和对应次数的表 where t1.cnt=(select max(t1.cnt) from t1) AND course.cid=t1.cid

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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