How to best display in Terminal a MySQL SELECT returning too many fields?
Terminate the query with \G
in place of ;
. For example:
SELECT * FROM sometable\G
This query displays the rows vertically, like this:
*************************** 1. row ***************************
Host: localhost
Db: mydatabase1
User: myuser1
Select_priv: Y
Insert_priv: Y
Update_priv: Y
...
*************************** 2. row ***************************
Host: localhost
Db: mydatabase2
User: myuser2
Select_priv: Y
Insert_priv: Y
Update_priv: Y
...
Try enabling vertical mode, using \G
to execute the query instead of ;
:
mysql> SELECT * FROM sometable \G
Your results will be listed in the vertical mode, so each column value will be printed on a separate line. The output will be narrower but obviously much longer.
Using mysql
's ego
command
From mysql
's help
command:
ego (\G) Send command to mysql server, display result vertically.
So by appending a \G
to your select
, you can get a very clean vertical output:
mysql> SELECT * FROM sometable \G
Using a pager
You can tell MySQL to use the less
pager with its -S
option that chops wide lines and gives you an output that you can scroll with the arrow keys:
mysql> pager less -S
Thus, next time you run a command with a wide output, MySQL will let you browse the output with the less
pager:
mysql> SELECT * FROM sometable;
If you're done with the pager and want to go back to the regular output on stdout
, use this:
mysql> nopager
You might also find this useful (non-Windows only):
mysql> pager less -SFX
mysql> SELECT * FROM sometable;
This will pipe the outut through the less
command line tool which - with these parameters - will give you a tabular output that can be scrolled horizontally and vertically with the cursor keys.
Leave this view by hitting the q
key, which will quit the less
tool.