How to export / dump a MySql table into a text file including the field names (aka headers or column names)

孤街醉人 提交于 2019-12-17 06:39:22

问题


In MySql's interpreter, it's very easy to dump a table to the screen along with its field names.

There seems to be no simple way to export a table to a tab-delimted or CSV outfile including its column headers.

I'm trying to do this using only SQL or the Linux command line, without writing a program in another language.

Thank you


回答1:


Piping the query to the commandline client outputs a tab separated list with the column names as the first line

$ echo "select * from surveys limit 5" | mysql -uroot -pGandalf surveys
phone   param1  param2  param3  param4  p0      p1      p2      p3      audio4  code    time
XXXXXXXXX       2008-07-02      11:17:23        XXXXXXXX        SAT     -       -       -       -       -       ERROR   2008-07-02 12:18:32
XXXXXXXXX       2008-07-02      11:22:52        XXXXXXXX        SAT     -       -       -       -       -       COLGADO 2008-07-02 12:04:29
XXXXXXXXX       2008-07-02      11:41:29        XXXXXXXX        SAT     -       -       -       -       -       COLGADO 2008-07-02 12:07:22
XXXXXXXXX       2008-07-02      12:16:19        XXXXXXXX        SAT     1       1       1       9       XXXXXXXXX_4.wav     OK      2008-07-02 16:14:27
XXXXXXXXX       2008-07-02      08:21:25        XXXXXXXX        SAT     1       1       1       1       XXXXXXXXX_4.wav     OK      2008-07-02 12:29:40



回答2:


This little script should do it:

-- 1. choose the table and the output file here / this should be the only input

select 'mytable' into @tableName;
select 'c://temp/test.csv' into @outputFile;

-- 2. get the column names in a format that will fit the query

select group_concat(concat("'",column_name, "'")) into @columnNames from information_schema.columns
where table_name=@tableName;

-- 3. build the query

SET @query = CONCAT(
"select * from
((SELECT ",@columnNames,")
UNION
(SELECT * FROM `",@tableName,"`)) as a
INTO OUTFILE '", @outputFile, "'");

-- 4. execute the query

PREPARE stmt FROM @query;
EXECUTE stmt;



回答3:


I achieved that in this way:

echo "select * from table"| mysql database -B -udbuser -puser_passwd | sed s/\\t/,/g > query_output.csv

The -B option of mysql separates the columns by tabs, which are converted into commas using sed. Note that the headers are generated too.




回答4:


You can do this with the mysqldump command. Have a look at the --tab and --xml options.




回答5:


I have created a procedure to automate the exporting of the contents of a larger number of tables to .csv file by using SELECT ... INTO OUTFILE. Please refer to the following if you have need for something like this

http://lifeboysays.wordpress.com/2012/06/23/mysql-how-to-export-data-to-csv-with-column-headers/.

It uses the method described by cafe876, but will work for one or a whole series of tables, plus you can set the delimiter and quote character to be used.




回答6:


I used the above command and modified according to my requirement.
I needed to get passwords column from the wordpress mysql database in a text file , to do that i used the following below command

echo "select user_pass from wp_users"| mysql -uroot -proot wp_database > passwordList.txt


来源:https://stackoverflow.com/questions/262924/how-to-export-dump-a-mysql-table-into-a-text-file-including-the-field-names-a

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