This is a website by Willem van Zyl

I'm a project manager, software developer, Apple evangelist and geek from South Africa. I'm passionate about web and mobile application development, usability, productivity, physics, astronomy, science fiction and fantasy.

If you would like to contact me, message me on Twitter or send me an email.

How to display MySQL CLI query output vertically

11 Apr 2009

The MySQL CLI (Command Line Interface) is a great tool to test queries and administrate remote (or local) databases.

Unless you have a large widescreen display or dual monitors, however, output from tables with a lot of columns break onto a new line, rendering the results unreadable.

For example, if you have a table called "location_cities" with a definition like this:

+---------------+--------------+------+-----+---------+
| FIELD         | Type         | NULL | KEY | DEFAULT |
+---------------+--------------+------+-----+---------+
| id            | varchar(255) | NO   | PRI |         |
| area_id       | varchar(255) | NO   | MUL |         |
| name          | varchar(255) | NO   |     |         |
| latitude      | varchar(100) | YES  |     | NULL    |
| longitude     | varchar(100) | YES  |     | NULL    |
| date_entered  | timestamp    | NO   |     |         |
| date_modified | timestamp    | YES  |     | NULL    |
+---------------+--------------+------+-----+---------+

... this query will render as follows:

SELECT * FROM location_cities;

Displaying the query results vertically instead of horizontally requires that you end your query line with "\G" instead of ";" (semicolon):

SELECT * FROM location_cities\G

There are several other modifiers that can be used in addition to or instead of a semicolon, to see a full list enter the "help" command while you are in the MySQL CLI:

?         (\?) Synonym for `help'.
clear     (\c) Clear command.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
Do you like this? Share it:

Copyright © Geekology 2011. All Rights Reserved.

Hosted by Code. Like. Clockwork.