22

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?

user1085195
  • 365
  • 2
  • 5
  • 16

3 Answers3

20

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';
rickydj
  • 629
  • 5
  • 17
  • 2
    If you've got a lot of rows in your source table, make sure you drop columns from the target table *before* populating the target table. – Erwin Wessels Jun 12 '18 at 11:38
  • can you dump all the data on the temp table with 1 or more column missing? like if table1 has 3 columns and temp_table1 has 2 columns, can you still dump the data? – Skeeith22 Dec 06 '19 at 04:00
  • how about if you duplicate certain columns only from database1 to database2? – Skeeith22 Dec 06 '19 at 12:53
9

Select the column into a file ?

Select col from table into outfile 'fileame'
SteveP
  • 18,840
  • 9
  • 47
  • 60
9
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`;
chiliNUT
  • 18,989
  • 14
  • 66
  • 106