Convert mysql query results to CSV (with copy/paste) -
i work in command line mysql. common need take query's results , import them numbers document (similar excel document).
what fastest method doing this?
method 1: select outfile
you can select outfile directly mysql, takes several steps.
- export query necessary arguments make csv format,
fields optionaly enclosed by
,delimited by
. - sftp server , grab file
- delete file server
method 2: copy/paste
i tend method. me seems little faster that's because don't remember how construct select outfile
query above , have up.
- copy/paste local text file
- open in text editor , replace | ,
- save csv , open in numbers.
how this?:
mysql -b -e "$my_query" > my_data.csv
the output format tab-separated rather comma-separated @ least excel , openoffice calc automatically adapt this.
btw, convenience , enable non-interactive execution of mysql commands, recommend setting secure ~/.my.cnf file
(readable you) entries this:
[client] user=your_mysql_user_name password=your_mysql_password host=your_mysql_server port=your_mysql_server_port whatever_other_options_you_like
references:
http://dev.mysql.com/doc/refman/5.1/en/mysql-command-options.html
--batch, -b
print results using tab column separator, each row on new line. option, mysql not use history file.
batch mode results in nontabular output format , escaping of special characters. escaping may disabled using raw mode; see description --raw option.
Comments
Post a Comment