If you’re getting an error that reads something like “ERROR 1136 (21S01): Column count doesn’t match value count at row 2” when using the VALUES
statement in MySQL, it’s probably because there’s a mismatch in the number of columns specified in the ROW()
clauses.
To fix this error, be sure that all ROW()
clauses contain exactly the same number of columns.
Example of Error
Here’s an example of code that will produce the error:
VALUES ROW(1, 2), ROW(3);
Result:
ERROR 1136 (21S01): Column count doesn't match value count at row 2
In this case, I passed two values with the first ROW()
but only one value with the second.
That’s what caused the error.
Solution
To fix the error, all we need to do is ensure that all ROW()
clauses contain the same number of values.
Example:
VALUES ROW(1, 2), ROW(3, 4);
Result:
+----------+----------+ | column_0 | column_1 | +----------+----------+ | 1 | 2 | | 3 | 4 | +----------+----------+
Other Causes of the Error
The same error can also occur when using the INSERT
statement to insert the wrong number of columns into a table.
In such cases, make sure you insert the correct number of rows. Alternatively, to insert data into fewer columns than the table contains, you can explicitly name the columns for which to insert data.