mysql delimiter question

前端 未结 4 1349
情书的邮戳
情书的邮戳 2020-12-19 12:14

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 c

相关标签:
4条回答
  • 2020-12-19 12:25
    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
    
    0 讨论(0)
  • 2020-12-19 12:25

    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' "
    
    0 讨论(0)
  • 2020-12-19 12:31

    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
    
    0 讨论(0)
  • 2020-12-19 12:40

    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 讨论(0)
提交回复
热议问题