I have been trying to update a table using the information in another
Here’s the query I am trying to use (I am not sure it’s correct)
INSERT INTO table1 SELECT * FROM table2 INNER JOIN table1 using (id);
when i try to run it, i get the error
ERROR 1136 (21S01): Column count doesn’t match value at row 1
The two tables are setup using the exact same create statement, except for the name of course, so I am not sure if this is a table error or a syntax error
CREATE TABLE table1 (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
fname TEXT NOT NULL,
lname TEXT NOT NULL,
city TEXT,
state TEXT,
zip INT,
eaddress TEXT NOT NULL,
reason TEXT NOT NULL,
message LONGTEXT NOT NULL
) DEFAULT CHARACTER SET utf8;
he’s selecting from table2 joined to table1, and inserting the results into table1, and that’s a non-starter because there are 2N columns in the dreaded, evil “select star”
your suggestion is to select N-1 columns (i.e. without the id) but the real question is what the join is supposed to be doing, and which of the 2N columns he really needs
What you need to do is get rid of the * in the SELECT, and instead specify each single column from table2 you want to insert in table1. Just make sure you specify the right number of values
suppose table1 has 37 rows, and table2 has 9 rows, of which 5 match a row in table1, and he wants to clone those 5 rows of table1 as new rows for table1?
i have learned (through bitter experience) that when people post questions about “table1” and “table2” (i.e. dumbed down or obfuscated), you cannot make any assumptions
if table1 and table2 each contain N columns, then the SELECT portion of your query (which uses the dreaded, evil “select star”) has 2N columns, which is clearly a case of “column count doesn’t match”
you are selecting all cols from one table (including the auto_increment id) and then trying to insert all those cols into the other table. Problem is, the second table is making its own auto_increment so the one from the first table is superfluous. select the cols you need from table 1 and insert them into table two. forget about selecting the id.