How to take mysql dump of selected columns of a table

扶醉桌前 提交于 2019-12-04 18:46:46

问题


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?


回答1:


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

create a temp table:

create table temp_table like name_of_the_original_table;

duplicating data into 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 -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';



回答2:


Select the column into a file ?

Select col from table into outfile 'fileame'



回答3:


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

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

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


来源:https://stackoverflow.com/questions/15264597/how-to-take-mysql-dump-of-selected-columns-of-a-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!