首先建表
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`id` int(11) NOT NULL,
`name` varchar(22) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('30001', '物理');
INSERT INTO `course` VALUES ('30002', '政治');
INSERT INTO `course` VALUES ('30003', '语文');
INSERT INTO `course` VALUES ('30004', '高数');
INSERT INTO `course` VALUES ('30005', '英语');
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(22) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('10001', 'tom');
INSERT INTO `student` VALUES ('10002', 'json');
INSERT INTO `student` VALUES ('10003', 'ak');
INSERT INTO `student` VALUES ('10004', 'km');
INSERT INTO `student` VALUES ('10005', 'mk');
-- ----------------------------
-- Table structure for student_course
-- ----------------------------
DROP TABLE IF EXISTS `student_course`;
CREATE TABLE `student_course` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`teacher_id` int(11) NOT NULL,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student_course
-- ----------------------------
INSERT INTO `student_course` VALUES ('4', '10001', '30002', '20002', '82');
INSERT INTO `student_course` VALUES ('6', '10001', '30003', '20003', '82');
INSERT INTO `student_course` VALUES ('8', '10001', '30004', '20005', '82');
INSERT INTO `student_course` VALUES ('10', '10001', '30005', '20005', '82');
INSERT INTO `student_course` VALUES ('12', '10002', '30001', '20001', '90');
INSERT INTO `student_course` VALUES ('14', '10002', '30002', '20002', '92');
INSERT INTO `student_course` VALUES ('16', '10002', '30003', '20003', '62');
INSERT INTO `student_course` VALUES ('18', '10002', '30004', '20004', '82');
INSERT INTO `student_course` VALUES ('20', '10002', '30005', '20005', '82');
INSERT INTO `student_course` VALUES ('22', '10003', '30001', '20001', '69');
INSERT INTO `student_course` VALUES ('24', '10003', '30002', '20002', '89');
INSERT INTO `student_course` VALUES ('26', '10003', '30003', '20003', '99');
INSERT INTO `student_course` VALUES ('28', '10003', '30004', '20004', '82');
INSERT INTO `student_course` VALUES ('30', '10003', '30005', '20005', '82');
INSERT INTO `student_course` VALUES ('32', '10004', '30001', '20001', '92');
INSERT INTO `student_course` VALUES ('34', '10004', '30002', '20002', '93');
INSERT INTO `student_course` VALUES ('36', '10004', '30003', '20003', '73');
INSERT INTO `student_course` VALUES ('38', '10004', '30004', '20004', '82');
INSERT INTO `student_course` VALUES ('40', '10004', '30005', '20005', '82');
INSERT INTO `student_course` VALUES ('42', '10005', '30001', '20001', '95');
INSERT INTO `student_course` VALUES ('44', '10005', '30002', '20002', '75');
INSERT INTO `student_course` VALUES ('46', '10005', '30003', '20003', '79');
INSERT INTO `student_course` VALUES ('48', '10005', '30004', '20004', '82');
INSERT INTO `student_course` VALUES ('50', '10005', '30005', '20005', '82');
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL,
`name` varchar(22) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('20001', 'su');
INSERT INTO `teacher` VALUES ('20002', 'wang');
INSERT INTO `teacher` VALUES ('20003', 'zhou');
INSERT INTO `teacher` VALUES ('20004', 'yang');
INSERT INTO `teacher` VALUES ('20005', 'liu');
4个表,包括student,course,student_course,teacher
现在把“student_course”表中“liu”老师教的课的成绩都更改为此课程的平均成绩
首先计算出”老师教的课的成绩的平均成绩
select avg(b.score) as score1 from teacher a,student_course b where a.name="liu" and b.teacher_id=a.id
然后再update里嵌套上面的select更新成绩
update student_course set score =(
select avg(b.score) as score1 from teacher a,student_course b where a.name="liu" and b.teacher_id=a.id
)
where teacher_id=(select id from teacher where name="liu");
但是运行后报错:
Error Code: 1093. You can't specify target table 'student_course' for update in FROM clause
因为update里嵌套select的时候,select的from后的目标表,不能是,update的目标表,上面update和select里的from都用到了student_course表,所以应该在select上再嵌套一个select,如下
update student_course set score =(
select score1 from (
select avg(b.score) as score1 from teacher a,student_course b where a.name="liu" and b.teacher_id=a.id
)
as tablename2
)where teacher_id=(select id from teacher where name="liu");
执行成功
6 row(s) affected Rows matched: 6 Changed: 6 Warnings: 0