Blog

What I Learned Today: MySQL Shell Commands

Written by Metal Toad Staff | Aug 23, 2013 12:00:00 AM
Filed under:

This is a quick one that I learned from a co-worker. He was in the MySQL shell and instead of ending his query with a semi-colon, like I normally use, he used \G. The results are interesting, instead of results showing in rows with the fields as columns, results are displayed vertically. Each record is separated by a row number and each field is on a newline with the field name.

"Normal" Query Syntax

SELECT * FROM `actions` LIMIT 3;

+--------------------------+---------+--------------------------+------------+-------------------+
| aid                      | type    | callback                 | parameters | label             |
+--------------------------+---------+--------------------------+------------+-------------------+
| comment_publish_action   | comment | comment_publish_action   |            | Publish comment   |
| comment_save_action      | comment | comment_save_action      |            | Save comment      |
| comment_unpublish_action | comment | comment_unpublish_action |            | Unpublish comment |
+--------------------------+---------+--------------------------+------------+-------------------+

Alternative Vertical Results

SELECT * FROM `actions` LIMIT 3\G

*************************** 1. row ***************************
       aid: comment_publish_action
      type: comment
  callback: comment_publish_action
parameters: 
     label: Publish comment
*************************** 2. row ***************************
       aid: comment_save_action
      type: comment
  callback: comment_save_action
parameters: 
     label: Save comment
*************************** 3. row ***************************
       aid: comment_unpublish_action
      type: comment
  callback: comment_unpublish_action
parameters: 
     label: Unpublish comment

Other Quick Commands

Also, you can use \g in place of a semi-colon. Another one I found on the MySQL command list page just now was \e, which opens your text editor and allows you to edit the query. I know I can use this when I mistype part of a query that I was breaking up into multiple lines.

This post is part of my challenge to never stop learning.