获取不带表格式的sql查询结果


68

--disable-column-namesoption一样,我们是否可以选择不使用表格式的sql查询?

例:

mysql -u username -ppassword --disable-column-names --execute "select name from test"

结果如下:

-----
| A |
| B |
| C |
| D |
-----

是否可以使用以下某些sql程序选项修饰符来获得查询结果,[没有表格式]

A
B
C
D

您可以使用SELECT .. INTO OUTFILE 'file_name' export_options
Faishal

Answers:


92

向中添加-B选项mysql

mysql -B -u username -ppassword \
      --disable-column-names \
      --execute "select name from mydb.test"

-B,-- batch :以非表格输出格式打印结果。

--execute:执行该语句并退出。

高温超导

编辑:感谢@ joescii,-B它是的缩写--batch,也启用了该--silent开关。


6
导出很不错-在mysql客户端中可以执行任何方法吗?
jsh 2014年

-b已弃用,请--batch改用。
Reactgular 2014年

2
无论如何从SQL脚本做到这一点?
Olayinka

--database <database-name>选项添加到命令行是一个好主意。
Krischu

27

尽管其他答案偶然起作用,但实际上正确的开关-s是的缩写--silent

您可能需要另外指定-r--raw输出,即禁用字符转义为好,否则换行符,制表符,零炭和反斜杠将分别被表示为\ N,\吨,\ 0和\。

   ·   --silent, -s

       Silent mode. Produce less output. This option can be given multiple
       times to produce less and less output.

       This option results in nontabular output format and escaping of
       special characters. Escaping may be disabled by using raw mode; see
       the description for the --raw option.

   ·   --raw, -r

       For tabular output, the “boxing” around columns enables one column
       value to be distinguished from another. For nontabular output (such
       as is produced in batch mode or when the --batch or --silent option
       is given), special characters are escaped in the output so they can
       be identified easily. Newline, tab, NUL, and backslash are written
       as \n, \t, \0, and \\. The --raw option disables this character
       escaping.

       The following example demonstrates tabular versus nontabular output
       and the use of raw mode to disable escaping:

           % mysql
           mysql> SELECT CHAR(92);
           +----------+
           | CHAR(92) |
           +----------+
           | \        |
           +----------+
           % mysql -s
           mysql> SELECT CHAR(92);
           CHAR(92)
           \\
           % mysql -s -r
           mysql> SELECT CHAR(92);
           CHAR(92)
           \

-甲骨文公司

MySQL 5.7 2018年7月7日

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.