Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MySQL's fancy table format as stdout

Tags:

mysql

stdout

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

+----+------+
| 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.

How can I get the fancy table format in stdout?

like image 842
a paid nerd Avatar asked Mar 16 '12 22:03

a paid nerd


2 Answers

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

$ echo "SELECT ..." | mysql -t | cowsay -n
 _______________
/ +----+------+ \
| | id | name | |
| +----+------+ |
| |  1 | Bob  | |
| |  2 | Mary | |
| |  3 | Jane | |
| |  4 | Lisa | |
\ +----+------+ /
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
like image 172
Christian Mann Avatar answered Oct 06 '22 01:10

Christian Mann


Try this:

$ mysql -uyourUsername -pyourPassword -te 'SELECT * FROM aTable' aDatabase
+----+------+
| id | name |
+----+------+
|  1 | Bob  |
|  2 | Mary |
|  3 | Jane |
|  4 | Lisa |
+----+------+
like image 35
Mosty Mostacho Avatar answered Oct 06 '22 02:10

Mosty Mostacho