Student(S_No,Sname,Sage,Ssex) 学生表
Course(C_No,Cname,T_No) 课程表
SC(S_No,C_No,score) 成绩表
Teacher(T_No,Tname) 教师表 把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩,我是mysql:我这么写可以更新叶平的课的成绩,不过不是他教的课的成绩全变成null了。
update SC set score = (
select avg_score from
(select sc2.C_No,avg(score) avg_score
from Teacher t ,course c,sc sc2 where t.T_no=c.T_no and Tname='李四' and sc2.C_No=c.C_No
group by sc2.C_No
) a where a.C_No=SC.C_No)
Course(C_No,Cname,T_No) 课程表
SC(S_No,C_No,score) 成绩表
Teacher(T_No,Tname) 教师表 把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩,我是mysql:我这么写可以更新叶平的课的成绩,不过不是他教的课的成绩全变成null了。
update SC set score = (
select avg_score from
(select sc2.C_No,avg(score) avg_score
from Teacher t ,course c,sc sc2 where t.T_no=c.T_no and Tname='李四' and sc2.C_No=c.C_No
group by sc2.C_No
) a where a.C_No=SC.C_No)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
/*
SQLyog Community Edition- MySQL GUI
MySQL - 5.1.42-community
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;create table `course` (
`C_No` double ,
`Cname` varchar (300),
`T_No` double
);
insert into `course` (`C_No`, `Cname`, `T_No`) values('1','Math','1');
insert into `course` (`C_No`, `Cname`, `T_No`) values('2','Chinese','1');
insert into `course` (`C_No`, `Cname`, `T_No`) values('3','Sport','3');
insert into `course` (`C_No`, `Cname`, `T_No`) values('4','Music','4');
create table `teacher` (
`T_No` double ,
`Tname` varchar (300)
);
insert into `teacher` (`T_No`, `Tname`) values('1','李四');
insert into `teacher` (`T_No`, `Tname`) values('2','王五');
insert into `teacher` (`T_No`, `Tname`) values('3','马六');create table `student` (
`S_No` double ,
`Sname` varchar (300),
`Sage` double ,
`Ssex` double
);
insert into `student` (`S_No`, `Sname`, `Sage`, `Ssex`) values('1','Mike','20','1');
insert into `student` (`S_No`, `Sname`, `Sage`, `Ssex`) values('2','Jack','21','1');
insert into `student` (`S_No`, `Sname`, `Sage`, `Ssex`) values('3','Jerry','22','1');
insert into `student` (`S_No`, `Sname`, `Sage`, `Ssex`) values('4','Tom','23','1');create table `sc` (
`S_No` double ,
`C_No` double ,
`score` double
);
insert into `sc` (`S_No`, `C_No`, `score`) values('1','1','96');
insert into `sc` (`S_No`, `C_No`, `score`) values('1','2','68');
insert into `sc` (`S_No`, `C_No`, `score`) values('2','1','80');
insert into `sc` (`S_No`, `C_No`, `score`) values('2','2','40');
insert into `sc` (`S_No`, `C_No`, `score`) values('3','4','94');
| S_No | C_No | score |
+------+------+-------+
| 1 | 1 | 88 |
| 1 | 2 | 54 |
| 2 | 1 | 88 |
| 2 | 2 | 54 |
| 3 | 4 | 94 |
+------+------+-------+李四教的课是1,2,所以其成绩全要变成平均成绩,分别是88,54
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0mysql> select * from sc;
+------+------+-------+
| S_No | C_No | score |
+------+------+-------+
| 1 | 1 | 88 |
| 1 | 2 | 54 |
| 2 | 1 | 88 |
| 2 | 2 | 54 |
| 3 | 4 | 94 |
+------+------+-------+
5 rows in set (0.00 sec)
(SELECT a.Tname,c.C_No,AVG(score) AS newscore FROM teacher a LEFT JOIN Course b
ON a.T_No=b.T_No
LEFT JOIN SC c ON b.C_No=c.C_No
LEFT JOIN Student d ON c.S_No=d.S_No
GROUP BY a.Tname,c.C_No) a3
ON a2.C_No=a3.c_no
SET a2.score=a3.newscore;
SELECT * FROM sc
-> select sc.C_No,avg(sc.score) as score
-> from course c inner join teacher t on c.T_No=t.T_N
-> inner join sc on c.C_No=sc.C_No
-> where t.Tname='李四'
-> group by sc.C_No
-> ) b on sc.C_No=b.C_No
-> set sc.score=b.score
-> ;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0mysql> select * from sc;
+------+------+-------+
| S_No | C_No | score |
+------+------+-------+
| 1 | 1 | 88 |
| 1 | 2 | 54 |
| 2 | 1 | 88 |
| 2 | 2 | 54 |
| 3 | 4 | 94 |
+------+------+-------+
5 rows in set (0.00 sec)mysql>
(
select sc2.c_no,avg(sc2.score) as avg_score
from sc sc2 ,teacher t,course c
where sc2.c_no=c.c_no and c.t_no=t.t_no and t.tname='李四'
group by sc2.c_no
) b
where sc.c_no=b.c_no
set sc.score=b.avg_score