Last Updated: February 25, 2016
·
787
· felipelavinz

Display MySQL results vertically on the command prompt

The mysql> prompt it's very useful for testing or quickly getting some info from a MySQL database.

Unfortunately, when the results for your query include rows with too much info to fit on the screen, the results display gets all messed up in a way that it's quite harder to find the specific info you're looking for:

mysql> select wp_1_postmeta.* from wp_1_postmeta inner join wp_1_posts on ID = post_id where ID = 123;
+---------+---------+----------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| meta_id | post_id | meta_key             |  meta_value                                                                                                                                                |
+---------+---------+----------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
|     212 |     123 | _wp_page_template    | default                                                                                                                                                   |
|     213 |     123 | _edit_lock           | 1357910478                                                                                                                                                |
|     214 |     123 | _edit_last           | 1                                                                                                                                                         |
|   22564 |     123 | _aioseop_title       | Misión SPI | Facultad de Psicología                                                                                                                   |
|   22560 |     123 | _links_tax           | -1                                                                                                                                                        |
|   22563 |     123 | _aioseop_description | La misión del SPI es entregar soluciones integrales a cada persona que requiera nuestros servicios, adecuándonos a sus necesidades. Infórmate aquí.
 |
+---------+---------+----------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
6 rows in set (0.01 sec)

But here comes the "\G" command, which will let you view the results on format that's easier to read for us humans:

mysql> select wp_1_postmeta.* from wp_1_postmeta inner join wp_1_posts on ID = post_id where ID = 123\G;
*************************** 1. row ***************************
meta_id: 212
post_id: 123
meta_key: _wp_page_template
meta_value: default
*************************** 2. row ***************************
meta_id: 213
post_id: 123
meta_key: _edit_lock
meta_value: 1357910478
*************************** 3. row ***************************
meta_id: 214
post_id: 123
meta_key: _edit_last
meta_value: 1
*************************** 4. row ***************************
meta_id: 22564
post_id: 123
meta_key: _aioseop_title
meta_value: Misión SPI | Facultad de Psicología  
*************************** 5. row ***************************
meta_id: 22560
post_id: 123
meta_key: _links_tax
meta_value: -1
*************************** 6. row ***************************
meta_id: 22563
post_id: 123
meta_key: _aioseop_description
meta_value: La misión del SPI es entregar soluciones integrales a cada persona que requiera nuestros servicios, adecuándonos a sus necesidades. Infórmate aquí.

6 rows in set (0.00 sec)

Just add \G to the end of your query and that's it!