我有这个查询插入行,使用如下的子查询:
INSERT INTO Lecture_presence_Student (`presence_id`, `Lecture_id`, `Student_id`, `status`) VALUES
(
(
SELECT '' as presence_id, Lecture.Lecture_id, CourseEdition_students_Student.Student_id, 'onverwerkt'
FROM
CourseEdition_students_Student
INNER JOIN Lecture ON CourseEdition_students_Student.CourseEdition_id = Lecture.CourseEdition_id
)
)
我没理解,子选择查询返回4列,与INSERT查询的编号相同。为什么它会给我错误:
Column count doesn't match value count at row 1
有什么想法吗?
答案 0 :(得分:5)
而不是使用INSERT INTO VALUES
,请使用INSERT INTO SELECT FROM
INSERT INTO Lecture_presence_Student
(
`presence_id`
, `Lecture_id`
, `Student_id`
, `status`
)
SELECT '' as presence_id
, Lecture.Lecture_id
, CourseEdition_students_Student.Student_id
, 'onverwerkt'
FROM CourseEdition_students_Student
INNER JOIN Lecture
ON CourseEdition_students_Student.CourseEdition_id = Lecture.CourseEdition_id
然后,如果您的查询中有多条记录,则INSERT
将有效。
答案 1 :(得分:1)
INSERT INTO Lecture_presence_Student (`presence_id`, `Lecture_id`, `Student_id`, `status`)
SELECT '' as presence_id, Lecture.Lecture_id, CourseEdition_students_Student.Student_id, 'onverwerkt'
FROM
CourseEdition_students_Student
INNER JOIN Lecture ON CourseEdition_students_Student.CourseEdition_id = Lecture.CourseEdition_id
presence_id
的空值是多少?
答案 2 :(得分:1)
INSERT INTO Lecture_presence_Student (`presence_id`, `Lecture_id`, `Student_id`, `status`)
SELECT '', Lecture.Lecture_id, CourseEdition_students_Student.Student_id, 'onverwerkt'
FROM
CourseEdition_students_Student
INNER JOIN Lecture ON CourseEdition_students_Student.CourseEdition_id = Lecture.CourseEdition_id
删除你的" VALUES(())"并且别名没有必要。