twitter
    Find out what I'm doing, Follow Me :)

Tuesday, November 19, 2013

MYSQL - How would I Export tables specifying only certain fields?

Backup mysql database table for specific date and time stamp using OUTFILE.

You need the FILE privilege to do this, and it won't overwrite files.
INTO OUTFILE has a bunch of options to it as well, such as FIELDS ENCLOSED BY, FIELDS ESCAPED BY, etc... that you may want to look up in the manual.
To produce a CSV file, you would do something like:

Example:SELECT A,B,C
INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM X;
1. dump mysql data and output

SELECT c_tEzaudit_ID, c_tEzaudit_op, c_tEzaudit_user, c_tEzaudit_orgid, c_tEzaudit_sno, c_tEzaudit_actby, c_tEzaudit_msg, c_tEzaudit_result, c_tEzaudit_create_time INTO OUTFILE '/tmp/result.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM tEzaudit where c_tEzaudit_create_time BETWEEN '2013-06-01 ' AND '2013-06-02';

OR

SELECT * INTO OUTFILE '/tmp/result1.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM tEzaudit where c_tEzaudit_create_time BETWEEN '2013-06-01 ' AND '2013-06-02';

2.To load the data back in from the file, use the LOAD DATA INFILE command with the same options you used to dump it out. For the CSV format above, that would be

Example:
LOAD DATA INFILE '/tmp/result.txt'
INTO TABLE X
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
LOAD DATA INFILE '/tmp/result.txt' INTO TABLE tEzaudit FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n';