What I Learned Today: MySQL Shell Commands

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
     label: Publish comment
*************************** 2. row ***************************
       aid: comment_save_action
      type: comment
  callback: comment_save_action
     label: Save comment
*************************** 3. row ***************************
       aid: comment_unpublish_action
      type: comment
  callback: comment_unpublish_action
     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.

Filed under:

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Ready for transformation?