[英]MySQL: How to insert all columns from table to new table and NULL for extra columns not matched?
Let me explain 让我解释
89 Columns
and 5 Million
rows. 89 Columns
和5 Million
行的MySQL表[Table A]。 10 new columns
(default NULL
) to [Table A]. 10 new columns
(默认为NULL
)。 Currently performing Alter Statement on [Table A] take more than 2 hours. I copied the DDL structure from [Table A] to [copied_Table A] as 我将DDL结构从[表A]复制到[复制表A],如下所示:
CREATE TABLE copied_table_a LIKE table_a;
The I added 10 columns
to copied_table_a
. 我在
copied_table_a
添加了10 columns
。
Now I want to insert all data from table_a
to copied_table_a
as 现在我想将所有数据从
table_a
插入到copied_table_a
中
mysql> INSERT copied_table_a SELECT * FROM table_a; ERROR 1136 (21S01): Column count doesn't match value count at row 1
Question 题
- What is the best way to get data now to copied_table_a
? -现在将数据获取到
copied_table_a
的最佳方法是什么?
I suggest that you list all columns, but you can do: 我建议您列出所有列,但是您可以执行以下操作:
INSERT INTO copied_table_a
SELECT a.*,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL
FROM table_a;
If you want a quick way to save typing you could make use of the information_schema.columns
table : 如果您想要一种快速的方法来保存键入内容,可以使用
information_schema.columns
表:
select concat("insert into copied_table_a (",
group_concat(column_name),
") select ",
group_concat(column_name)," from ",table_name)
from information_schema.columns
where table_name = 'table_a';
To generate the SQL for you. 为您生成SQL。 Only really good for a one-off but quite fun to play around with.
一次过真的很不错,但是很有趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.