我尝试使用以下查询在表中插入数据并出现错误
insert into filmo_person_song (person_id, song_id, role_id)
select person_id
from filmo_person_song fps, filmo_song fs, filmo_role fr
where fps.song_id = fs.song_id
and fps.role_id = fr.role_id
and fps.person_id = 43629;
ERROR 1136(21S01):列数与第1行的值计数不匹配
我已准确指定了字段..
答案 0 :(得分:8)
您尝试在每行中插入一个值,但您已指定要写入的三个列:
insert into filmo_person_song (person_id, song_id, role_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
select person_id
^^^^^^^^^
您需要列出select语句中所有三列的值。以下可能有效:
insert into filmo_person_song (person_id, song_id, role_id)
select fps.person_id, fs.song_id, fr.role_id
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
答案 1 :(得分:2)
您只是从子查询中选择person_id
,而在insert子句中指定了person_id
,song_id
和role_id
。您还必须在子查询中选择缺少的字段。
你可能想要这样的东西:
INSERT INTO
filmo_person_song (person_id, song_id, role_id)
SELECT
person_id,
song_id,
role_id
FROM
filmo_person_song fps, filmo_song fs, filmo_role fr
WHERE
fps.song_id = fs.song_id
AND
fps.role_id = fr.role_id
AND
fps.person_id = 43629;
答案 2 :(得分:2)
您只是指定要插入表格的一个字段(select person_id
),而列规范说明person_id
,sond_id
和 {{将插入1}}。
这应该会更好:
role_id
答案 3 :(得分:1)
将其更改为:
insert into filmo_person_song (person_id, song_id, role_id)
select person_id, fs.song_id, fr.role_id
from filmo_person_song fps, filmo_song fs, filmo_role fr
where fps.song_id = fs.song_id
and fps.role_id = fr.role_id
and fps.person_id = 43629;
答案 4 :(得分:1)
子句返回值必须与插入查询匹配,如下所示。
insert into filmo_person_song (person_id, song_id, role_id)
select person_id, song_id, role_id
from filmo_person_song fps, filmo_song fs, filmo_role fr
where fps.song_id = fs.song_id
and fps.role_id = fr.role_id
and fps.person_id = 43629;
答案 5 :(得分:1)
您要求它为3列添加值(person_id,song_id,role_id),但只提供1:
为person_id
尝试:
insert into filmo_person_song (person_id)
values (select person_id
from filmo_person_song fps, filmo_song fs, filmo_role fr
where fps.song_id = fs.song_id
and fps.role_id = fr.role_id
and fps.person_id = 43629);
或:
insert into filmo_person_song (person_id, song_id, role_id)
values (
select person_id, song_id, role_id
from filmo_person_song fps, filmo_song fs, filmo_role fr
where fps.song_id = fs.song_id
and fps.role_id = fr.role_id
and fps.person_id = 43629);