我正在创建一个存储过程,以将数据插入到三个不同的表中。
这是我的代码:
/*------------------------- Procedure for owner to submit his property -------------------------*/
DELIMITER //
CREATE PROCEDURE SubmitProperty (
IN input_property_owner_id INT,
IN input_property_type_id INT,
IN input_address VARCHAR(255),
IN input_zip_code VARCHAR(255),
IN input_area_m2 INT,
IN input_price_€ INT
)
BEGIN
INSERT INTO property (property_owner_id, property_type_id, address, zip_code, area_m2, price_€)
VALUES
(input_property_owner_id, input_property_type_id, input_address, input_zip_code, input_area_m2, input_price_€);
INSERT INTO survey (property_id, cas_eval_id, checksum_xxx)
VALUES
(property.id, 'CAS-XXX-YYYY', property.id % 1000);
INSERT INTO survey_question_answer (survey_id, question_id)
SELECT property_type_question.question_id
FROM property_type_question
WHERE property_type_question.property_type_id = input_property_type_id
VALUES
(survey.id, property_type_question.question_id);
END //
DELIMITER ;
前2个插件正常工作。
但是,我在第三个插入中出现错误(在最后一个“ FROM”和“ VALUES”之间的区域附近)。
这是错误消息的图片:
你们可以帮我解决这个问题吗?
谢谢!
答案 0 :(得分:0)
是因为您试图插入两列,但只选择了其中一列?
INSERT INTO survey_question_answer (survey_id, question_id)
SELECT property_type_question.question_id
答案 1 :(得分:0)
survey_id是身份列吗?如果不是,则需要从Survey表中引入survey_id并将其添加到您的select子句中:
INSERT INTO survey_question_answer (survey_id , question_id)
SELECT survey.id, property_type_question.question_id
FROM property_type_question
//Join here with your survey table
WHERE property_type_question.property_type_id = input_property_type_id;