如何将 MySql 表导出/转储到包含字段名称(又名标题或列名称)的文本文件中

2022-01-11 00:00:00 field header export mysql

在 MySql 的解释器中,很容易将表格连同其字段名一起转储到屏幕上.

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

似乎没有简单的方法可以将表格导出到制表符分隔或 CSV 输出文件包括其列标题.

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

我正在尝试仅使用 SQL 或 Linux 命令行来执行此操作,而不用另一种语言编写程序.

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

谢谢

推荐答案

将查询传递到命令行客户端会输出一个制表符分隔的列表,其中列名作为第一行

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

相关文章