如何对表的选定列进行 mysql 转储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15264597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:47:45  来源:igfitidea点击:

How to take mysql dump of selected columns of a table

mysqlmysql5

提问by user1085195

I have a requirement in which I have to take mysql dump of just one column of a table. Since that table has too many columns, I don't want to take dump of the full table. I have to get this dump of the table from one server to another. Any idea how I can do this?

我有一个要求,我必须对表的一列进行 mysql 转储。由于该表有太多列,我不想转储整个表。我必须将这个表的转储从一台服务器转移到另一台服务器。知道我该怎么做吗?

回答by rickydj

If you would like to take mysql dump including the schema, it can be done by following the below steps:

如果您想使用包含架构的 mysql 转储,可以按照以下步骤完成:

create a temp table:

创建临时表:

create table temp_table like name_of_the_original_table;

duplicating data into temp_table:

将数据复制到 temp_table 中:

insert into temp_table select * from name_of_the_original_table;

dropping unnecessary fields:

删除不必要的字段:

alter table temp_table drop column somecolumn;

post this, you could take a mysqldump by running:

发布这个,你可以通过运行来获取一个 mysqldump:

mysqldump -u <username> -p <password> databasename temp_table

If the intention is to take a data dump(without the schema), you can run the below command:

如果打算进行数据转储(没有架构),您可以运行以下命令:

select * from sometable into outfile '/tmp/datadump' fields terminated by '\t' lines terminated by '\n';

回答by SteveP

Select the column into a file ?

选择列放入文件 ?

Select col from table into outfile 'fileame'

回答by chiliNUT

mysql> CREATE TABLE `tempTable` AS SELECT `columnYouWant` from `table`;
$> mysqldump yourDB tempTable > temp.sql

copy temp.sql to target server, then on target server

将 temp.sql 复制到目标服务器,然后复制到目标服务器上

$> mysql yourDB < temp.sql
mysql> RENAME TABLE `table` TO `tableBackup`, `tempTable` TO `table`;