1

I've read and searched for the answer to this before asking. I am trying to import the data of 4 columns from one MYSQL table into another MYSQL table that has more than 4 columns. As I expected I got

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

Does anyone know a trick to get around this issue?

Postscript

I've already tried using

INSERT INTO new_table(colname1,colname2,colname3,colname4)
SELECT 'colname1','colname2','colname3,'colname4' FROM old_table;

that generates error

1054 unknown column 'colname1' in 'field list

I've also tried

INSERT INTO new_table 
SELECT 'colname1,'colname2,'colname3','colname4' 
FROM old_table;

which generates error

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

4

3 回答 3

3

If you are trying to INSERT INTO a table with a different number of columns, then you should specify what columns you want to insert into.

So if you have a table with 20 columns and you know you want to insert into column 1, column 10, column 15 and column 20, then you explicitly name those columns in your insert.

Similar to this:

INSERT INTO Table2 (col1, col10, col15, col20)
SELECT col1, col2, col3, col4
FROM Table1
于 2013-03-28T18:31:19.287 回答
1

您需要指定要插入的列

insert into destination_table (col1, col2, col3, col4)
select * from source_table

想一想:数据库引擎应该如何知道哪个源列必须填充哪个目标列?

SQLFiddle 演示

更新

删除列名周围的引号

INSERT INTO new_table(colname1, colname2, colname3, colname4)
SELECT colname1, colname2, colname3, colname4 
FROM old_table;

如果要转义列名,请改用反引号 ( `)。

于 2013-03-28T18:31:41.527 回答
0
INSERT INTO new_table(colname1,colname2,colname3,colname4)
SELECT 'colname1','colname2','colname3,NULL FROM old_table;

只要在没有可匹配的列的地方输入 NULL

于 2014-06-25T06:57:28.030 回答