Hi! I have a MySQL table that needs to be exported into several separate files. The table should be grouped by a particular column and files should have names of the corresponding values of this column. Format is not relevant I just need a suitable technique, program, whatever. Any help would be much appreciated!
+1
A:
If it's less than 10 files or so, it's easy to manually craft a script like:
SELECT *
FROM YourTable
WHERE col1 = 'alfa'
INTO OUTFILE 'c:\result-alfa.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
If typing it out is too tedious, consider a query like this to generate the script:
SELECT concat('SELECT * FROM YourTable WHERE col1 = ''',
col1, ''' INTO OUTFILE '''c:\result-', col1, '.txt'' ',
'FIELDS TERMINATED BY '','' OPTIONALLY ENCLOSED BY ''"''',
'LINES TERMINATED BY ''\n'';')
FROM YourTable
GROUP BY col1
Andomar
2009-12-19 18:57:07
thanks, it worked for me! Not so elegant but works.Btw FROM and WHERE clause should precede INTO OUTFILE
2009-12-19 19:18:31
@warden: Right, I'll edit the answer!
Andomar
2009-12-19 19:24:48