2 回答
TA贡献9条经验 获得超8个赞
你这个sc1.grade 给它命名别名了吗,又没有别名又没有定义MySQL引擎语法上肯定不识别啊
你可以参考下这样写就可以了。MySQL 已经执行成功的,以下代码可以直接复制你查询实现下:
create table sc(
sno varchar(20) primary key not null,
cno varchar(20),
grade float(6,2) unsigned
);
insert into sc values('a001','c01',88.5);
insert into sc values('a002','c01',78.5);
insert into sc values('a002','c01',78.5);
insert into sc values('a003','c01',68.5);
insert into sc values('a004','c01',58.5);
insert into sc values('a005','c01',18.5);
insert into sc values('a006','c02',18.5);
insert into sc values('a007','c02',28.5);
insert into sc values('a008','c02',38.5);
insert into sc values('a009','c02',78.5);
insert into sc values('a010','c03',78.5);
insert into sc values('a011','c03',48.5);
insert into sc values('a012','c03',49.5);
insert into sc values('a013','c04',100);
insert into sc values('a014','c04',90);
mysql> update sc set grade=(select sc1_avg_grade from((select avg(grade) as sc1_
avg_grade from sc where cno='c01' group by cno) as sc1) ) where cno='c01';
Query OK, 5 rows affected (0.04 sec)
Rows matched: 5 Changed: 5 Warnings: 0
采纳的话别忘记给分哟,亲
TA贡献9条经验 获得超8个赞
简单理解为avg(grade) as sc1_avg_grade,为平均成绩avg(grade)命名别名sc1_avg_grade,
同时(select avg(grade) as sc1_avg_grade from sc where cno='c01' group by cno) as sc1 为这个结果集的表也命名个别名sc1,
查询这个别名即可,对这个sc1表进行操作即可,就不会冲突
添加回答
举报