获取没有表格格式的sql查询结果

Joh*_*ohn 58 mysql sql shell

--disable-column-names选项一样,我们有一个选项来获取没有表格式的SQL查询吗?

例:

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

结果如下:

-----
| A |
| B |
| C |
| D |
-----
Run Code Online (Sandbox Code Playgroud)

是否有可能使用如下的一些sql程序选项修饰符来获取查询结果,[没有表格格式]

A
B
C
D
Run Code Online (Sandbox Code Playgroud)

Har*_*pta 81

添加-B选项mysql.

mysql -B -u username -ppassword \
      --disable-column-names \
      --execute "select name from mydb.test"
Run Code Online (Sandbox Code Playgroud)

-B, - block :以非表格输出格式打印结果.

--execute:执行语句并退出.

HTH

编辑:感谢@ joescii,-B这是简称--batch,也启用了--silent切换.

  • 很适合导出 - 在mysql客户端中以任何方式执行此操作? (5认同)
  • 它的工作原理是因为它启用了`--silent`开关. (4认同)
  • 无论如何从sql脚本做到这一点? (2认同)

Rio*_*iot 23

虽然其他答案偶然起作用,但正确的开关实际上-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)
           \
Run Code Online (Sandbox Code Playgroud)

- 甲骨文公司

MySQL 5.7 06/07/2018