from后面给表起了别名,但是后面的where中用不了

select Student.Sname, t2.SId, t2.avgsc
from 
    Student,
    (select t1.SId SId,avg(t1.score) avgsc from (select SId, score  from SC where score < 60) t1 group by t1.SId)  t2
where 
    Student.SId in (
        select SId from 
        (select t1.SId SId, avg(t1.score) avgsc from (select SId,  score from SC where score < 60) t1 group by t1.SId) t2
    );

 t2在

Student.SId in (select SId from (select t1.SId SId,avg(t1.score) avgsc
from
    (select SId,score from SC where score < 60) t1  group by t1.SId) t2);

 这一段不能直接使用中间表别名

where Student.SId in (select SId from t2)  会报错

 

你这个t2是子查询内部的别名,这样不行的。放在上面的字段不可以是子查询的别名。

SELECT Student.Sname, 
     t2.SId,
        t2.avgsc
FROM  Student,
       (SELECT t1.SId SId,AVG(t1.score) avgsc FROM (SELECT SId, score  FROM SC WHERE score < 60) t1 GROUP BY t1.SId)  t2
WHERE 
    Student.SId =t2.SId;
效果一样吧 个人感觉不用把那个子查询出现两次吧

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y