Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert query with a subquery

i tried to use the following query to insert data in a table and got an error

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): Column count doesn't match value count at row 1

i have specified the fields exactly..

like image 839
devang Avatar asked Nov 22 '25 04:11

devang


2 Answers

You are trying to insert one value in each row, but you have specified three columns to be written to:

insert into filmo_person_song (person_id, song_id, role_id)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
select person_id
       ^^^^^^^^^

You will need to list values for all of the three columns in your select statement. The following might work:

insert into filmo_person_song (person_id, song_id, role_id)
select fps.person_id, fs.song_id, fr.role_id
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
like image 153
Jørn Schou-Rode Avatar answered Nov 24 '25 01:11

Jørn Schou-Rode


You are only selecting person_id from the subquery, whereas you specified person_id, song_id and role_id in the insert clause. You must select also the missing fields in the subquery.

You probably want something like this:

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;
like image 41
Tatu Ulmanen Avatar answered Nov 23 '25 23:11

Tatu Ulmanen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!