将表导出到按列分组的文件中

时间:2009-12-19 18:48:54

标签: mysql export

我有一个MySQL表需要导出到几个单独的文件中。该表应按特定列分组,文件应具有此列的相应值的名称。格式不相关我只需要一个合适的技术,程序,等等。 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

如果它少于10个文件左右,则可以轻松手动制作如下脚本:

SELECT *
FROM YourTable
WHERE col1 = 'alfa'
INTO OUTFILE 'c:\result-alfa.txt' 
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 
LINES TERMINATED BY '\n' 

如果输入它太繁琐,请考虑这样的查询来生成脚本:

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