Problem Solution Discussion Exporting Query Results from MySQL

t ell it t o skip colum ns in t he file. To deal wit h t his difficult y, we can preprocess t he input file int o a t em porary file t hat doesnt cont ain t he password value, t hen load t he t em porary file. Under Unix, you can use t he cut ut ilit y t o ext ract t he colum ns t hat you want , like t his: cut -d: -f0,3- etcpasswd passwd.txt The - d opt ion specifies a field delim it er of : and t he - f opt ion indicat es t hat you want t o cut colum n one and all colum ns from t he t hird t o t he end of t he line. The effect is t o cut all but t he second colum n. Run m an cut for m ore inform at ion about t he cut com m and. Then use LOAD DATA t o im port t he result ing passwd.t xt file int o t he passwd t able like t his: mysql LOAD DATA LOCAL INFILE passwd.txt INTO TABLE passwd - FIELDS TERMINATED BY :; The corresponding m ysqlim port com m and is: mysqlimport --local --fields-terminated-by=: cookbook passwd.txt 10.13.4 See Also cut always displays out put colum ns in t he sam e order t hey occur in t he file, no m at t er what order you use when you list t hem wit h t he - f opt ion. For exam ple, cut - f1,2,3 and cut - f3,2,1 produce t he sam e out put . Recipe 10.20 discusses a ut ilit y t hat can pull out and display colum ns in any order.

10.14 Exporting Query Results from MySQL

10.14.1 Problem

You want t o export t he result of a query from MySQL int o a file or anot her program .

10.14.2 Solution

Use t he SELECT ... INTO OUTFILE st at em ent or redirect t he out put of t he m ysql program .

10.14.3 Discussion

MySQL provides a SELECT ... INTO OUTFILE st at em ent t hat export s a query result direct ly int o a file on t he server host . Anot her way t o export a query, if you want t o capt ure t he result on t he client host inst ead, is t o redirect t he out put of t he m ysql program . These m et hods have different st rengt hs and weaknesses, so you should get t o know t hem bot h and apply whichever one best suit s a given sit uat ion. 10.14.4 Exporting with the SELECT ... INTO OUTFILE Statement The synt ax for t his st at em ent com bines a regular SELECT w it h INTO OUTFILE filename at t he end. The default out put form at is t he sam e as for LOAD DATA , so t he following st at em ent export s t he passwd t able int o t m p passwd.t xt as a t ab-delim it ed, linefeed- t erm inat ed file: mysql SELECT FROM passwd INTO OUTFILE tmppasswd.txt; You can change t he out put form at using opt ions sim ilar t o t hose used wit h LOAD DATA t hat indicat e how t o quot e and delim it colum ns and records. To export t he passwd t able in CSV form at wit h CRLF- t erm inat ed lines, use t his st at em ent : mysql SELECT FROM passwd INTO OUTFILE tmppasswd.txt - FIELDS TERMINATED BY , ENCLOSED BY - LINES TERMINATED BY \r\n; SELECT ... INTO OUTFILE has t he following propert ies: • The out put file is creat ed direct ly by t he MySQL server, so t he filenam e should indicat e where you want t he file t o be writ t en on t he server host . There is no LOCAL version of t he st at em ent analogous t o t he LOCAL version of LOAD DATA . • You m ust have t he MySQL FILE privilege t o execut e t he SELECT ... INTO st at em ent . • The out put file m ust not already exist . This prevent s MySQL from clobbering files t hat m ay be im port ant . • You should have a login account on t he server host or som e way t o ret rieve t he file from t hat host . Ot herwise, SELECT ... INTO OUTFILE likely will be of no value t o you. • Under Unix, t he file is creat ed world readable and is owned by t he MySQL server. This m eans t hat alt hough youll be able t o read t he file, you m ay not be able t o delet e it .

10.14.5 Using the mysql Client to Export Data