MySQL how do you append to a file with INTO OUTFILE?

心不动则不痛 提交于 2019-12-08 18:04:58

问题


I have the following code:

SELECT * INTO OUTFILE'~/TestInput/Results.csv'      
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
FROM Results;

Desired results are to continually keep on appending to Results.csv


回答1:


You can merge results and write at once. TEE will log everything which might not be desirable. In your case:

SELECT * FROM Results UNION 
SELECT * FROM Results INTO OUTFILE '~/TestInput/Results.csv';



回答2:


You can't do that with SELECT INTO OUTFILE. That command only creates new files, and will fail if the file already exists.

You can append query output to an existing files using the TEE command in the MySQL client.

Here's your example appending two query results to the same file using TEE:

TEE ~/TestInput/Results.csv

SELECT * 
FROM Results;

SELECT * 
FROM Results;

NOTEE



回答3:


It is not possible to do it directly in MySQL. But you may try to add date-time part into file name, then combine some files to a new one with a 'cat' (UNIX command) or 'type' (DOS command).

Help: cat (Unix)



来源:https://stackoverflow.com/questions/9721175/mysql-how-do-you-append-to-a-file-with-into-outfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!