我是 mysql 中的存储程序,使用insert into..select
语句时我需要在额外的列中数据。
这是我的伪代码
@the_last_insert_id = last_insert_id
@the_class_id = select class_id where examination_id = @the_last_insert_id;
@the_year_id = select year_id where examination_id = @the_last_insert_id;
insert into examination_data (ed_cs_id,ed_examination_id) select cs_id from class_students where cs_class_id = @the_class_id AND cs_year_id = @the_year_id,@the_last_insert_id;
这是在列中插入数据的正确方法ed_examination_id
吗?
更新:架构
CREATE TABLE class_students (
cs_id INT NOT NULL AUTO_INCREMENT,
cs_class_student int NOT NULL,
cs_class_id int NOT NULL,
cs_year_id int NOT NULL,
PRIMARY KEY (cs_id),
FOREIGN KEY (cs_class_student) REFERENCES students(student_id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (cs_class_id) REFERENCES classes(class_id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (cs_year_id) REFERENCES years(year_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE examinations (
examination_id INT NOT NULL AUTO_INCREMENT,
examination_name varchar(255) NOT NULL,
examination_term_id int NOT NULL,
examination_class_id int NOT NULL,
examination_class_year_id int NOT NULL,
PRIMARY KEY (examination_id),
FOREIGN KEY (examination_term_id) REFERENCES terms(term_id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (examination_class_id) REFERENCES classes(class_id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (examination_class_year_id) REFERENCES years(year_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE examination_data (
ed_id INT NOT NULL AUTO_INCREMENT,
ed_cs_id int NOT NULL,
ed_examination_id int NOT NULL,
mathematics int NOT NULL,
music int NOT NULL,
drawing int NOT NULL,
swimming int NOT NULL,
dance int NOT NULL,
mechanics int NOT NULL,
programming int NOT NULL,
totals int NOT NULL,
class_rank int NOT NULL,
PRIMARY KEY (ed_id),
FOREIGN KEY (ed_cs_id) REFERENCES class_students(cs_id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (ed_examination_id) REFERENCES examinations(examination_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;