Exporting a MySQL table into a CSV file

前端 未结 4 1168
孤独总比滥情好
孤独总比滥情好 2020-12-14 01:45

I have a MySQL table which has to be taken out as a CSV file. The query I used is

SELECT \"ID\",\"NAME\",\"SALARY\",\"SAL1\",\"SAL2\",\"SAL3\",\"SAL4\",\"SAL         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-14 02:01

    This command almost gives you what you want, and it even works with a remote server. The only caveat is that it generates a TSV file (fields are separated by a tab).

    mysql mydb -e "select * from mytable" -B > mytable.tsv 
    

    But you could convert it to CSV using sed, as suggested in this answer:

    mysql mydb -e "select * from mytable" -B | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > mytable.csv
    

提交回复
热议问题