MySQL,使用值作为select语句的Insert语句

时间:2018-04-08 03:11:01

标签: mysql database select insert

insert into class_pre_req(class_id, cpr_id) values(select class_id from classes where catlg_nbr = 265 and subject_id = 27, select class_id from classes where catlg_nbr = 166 and subject_id = 27);

我正在尝试使用值作为插入语句插入到此表中。我假设我只是使用了错误的语法。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

VALUES仅在插入文字记录时使用。相反,只需插入一个由两个子查询组成的select语句:

INSERT INTO class_pre_req (class_id, cpr_id)
SELECT
    (SELECT class_id FROM classes WHERE catlg_nbr = 265 AND subject_id = 27),
    (SELECT class_id FROM classes WHERE catlg_nbr = 166 AND subject_id = 27);

请注意,为了使其工作,每个子查询都必须返回一个值。