开发者

mysql delimiter question

开发者 https://www.devze.com 2023-01-17 12:19 出处:网络
How do i do this: mysql -u myuser -p mydb -e \"select f1,f2 frommytable\" > /tmp/mydata.txt But I want to separate the fields with a comma.

How do i do this:

mysql -u myuser -p mydb -e "select f1,f2 from  mytable" > /tmp/mydata.txt

But I want to separate the fields with a comma.

mysql --delimiter=, -u myuser -p mydb -e "select f1,f2 from  mytable" > /tmp/开发者_如何学Cmydata.txt

Does not work :(


I don't really understand what is the question? Can you explain what you want ?

If your wish is to add delimiter to your output, you have to use CONCAT_WS :

mysql -u myuser -p mydb -e "select CONCAT_WS(',',f1,f2) from mytable" > /tmp/mydata.txt


you can use INTO OUTFILE like this :

mysql  -u myuser -p  mydatabase -e 
    "select field1 , field2 FROM mytable INTO OUTFILE 
'/tmp/myfilename.csv' FIELDS TERMINATED BY ','
 ENCLOSED BY '\"' LINES TERMINATED BY '\n' "


SELECT INTO OUTFILE

requires ** FILE ** permissions for mysql user https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html (permission to write on mysql host filesystem). It's vulnerability issue and therefore database's ** ALL PRIVILEGES ** not includes FILE permissions.

In most cases better use pipeline replacement from tabulation symbols \t to any character you want ,:

mysql -h -p dbname -e 'SELECT * FROM table_name;' | sed 's/\t/,/g' > out.csv


You might also try the SELECT INTO OUTFILE command to produce your CSV file. Info here: http://dev.mysql.com/doc/refman/5.1/en/select.html

0

精彩评论

暂无评论...
验证码 换一张
取 消