将MySQL的花式表格格式化为stdout

时间:2021-07-06 00:05:19

When I do a SELECT in MySQL's command-line interpreter I get a sweet ASCII table:

当我在MySQL的命令行解释器中执行SELECT时,我得到一个甜蜜的ASCII表:

+----+------+
| id | name |
+----+------+
|  1 | Bob  |
|  2 | Mary |
|  3 | Jane |
|  4 | Lisa |
+----+------+

However, if I pipe the result somewhere (e.g., echo "SELECT ..." | mysql), I get a boring old tab-delimited result.

但是,如果我将结果传递到某处(例如,echo“SELECT ...”| mysql),我会得到一个无聊的旧制表符分隔结果。

How can I get the fancy table format in stdout?

如何在stdout中获得精美的表格格式?

2 个解决方案

#1


25  

You can use the -t or --table switch on the mysql command, e.g.

您可以在mysql命令上使用-t或--table开关,例如:

$ echo "SELECT ..." | mysql -t | cowsay -n
 _______________
/ +----+------+ \
| | id | name | |
| +----+------+ |
| |  1 | Bob  | |
| |  2 | Mary | |
| |  3 | Jane | |
| |  4 | Lisa | |
\ +----+------+ /
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

#2


3  

Try this:

尝试这个:

$ mysql -uyourUsername -pyourPassword -te 'SELECT * FROM aTable' aDatabase
+----+------+
| id | name |
+----+------+
|  1 | Bob  |
|  2 | Mary |
|  3 | Jane |
|  4 | Lisa |
+----+------+

#1


25  

You can use the -t or --table switch on the mysql command, e.g.

您可以在mysql命令上使用-t或--table开关,例如:

$ echo "SELECT ..." | mysql -t | cowsay -n
 _______________
/ +----+------+ \
| | id | name | |
| +----+------+ |
| |  1 | Bob  | |
| |  2 | Mary | |
| |  3 | Jane | |
| |  4 | Lisa | |
\ +----+------+ /
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

#2


3  

Try this:

尝试这个:

$ mysql -uyourUsername -pyourPassword -te 'SELECT * FROM aTable' aDatabase
+----+------+
| id | name |
+----+------+
|  1 | Bob  |
|  2 | Mary |
|  3 | Jane |
|  4 | Lisa |
+----+------+