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

时间:2013-05-23 10:31:27

标签: mysql sql shell

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

示例:

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

结果如下:

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

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

A
B
C
D

3 个答案:

答案 0 :(得分:77)

-B选项添加到mysql

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

-B - 批次:以非常规输出格式打印结果。

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

HTH

编辑:感谢@ joescii,-B,它是--batch的缩写,也启用--silent切换。

答案 1 :(得分:22)

虽然其他答案偶然起作用,但正确的切换实际上是-s,这是--silent的缩写。

您可能还想为-r输出另外指定--raw,这也会禁用字符转义,否则换行符,制表符,空字符和反斜杠将表示为\ n,\ t,\ 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)
           \
     

- Oracle Corporation

     

MySQL 5.7 06/07/2018

答案 2 :(得分:1)

可以将输出传输到文件,而不使用边框。

mysql -u username -ppassword --disable-column-names --execute“select name from test”> TESTFILE