Clint Clint - 1 month ago 7
MySQL Question

Inserts MySQL when column count doesn't match

I'm trying to insert into a table. My table has id (PK), column1, column2, column 3, column4.

Columns 3 and 4 are from a join and present in table2.

My SQL looks as follows

INSERT INTO table3 (SELECT column1 , column2, column3, column4 FROM `table1` LEFT JOIN table2 ON table2.id = table1.id)


but I am getting the following error

1136 - Column count doesn't match value count at row 1



I can see this is because I do not have the id in my select. I am unsure how i am supposed to do this as I want the id (pk) to be auto generated.

Can someone please point out what I am doing wrong

Answer

I am not sure but you should try to query like this

INSERT INTO table3(column1,column2,column3,column4) SELECT column1 , column2, column3, column4 FROM `table1` LEFT JOIN table2 ON table2.id = table1.id
Comments