Archive for January, 2010

Display MySQL query output on the command line

Tuesday, January 19th, 2010

If you are using the MySQL command line client regularly, the following scenario might be familiar to you: You run a select on a table, which contains a lot of fields. The output will not fit on one line, which means that it will be unreadable. Consider the following example:

  1. mysql> SELECT * FROM posts LIMIT 1;
  2. +----+-------------+---------------------+---------------------+--------------------------------+--------------+-----------
  3. ----+--------------+----------+----------+-------------+----------------+-------------+---------------+-------------+----
  4. -----+--------+---------------------+---------------------+-----------------------+-------------+-------+------------+----
  5. -------+----------------+---------------+
  6. | ID | post_author | post_date | post_date_gmt | post_content | post_title |
  7. post_category | post_excerpt | post_lat | post_lon | post_status | comment_status | ping_status |
  8. post_password | post_name | to_ping | pinged | post_modified | post_modified_gmt |
  9. post_content_filtered | post_parent | guid | menu_order | post_type | post_mime_type |
  10. comment_count |

  11. +----+-------------+---------------------+---------------------+--------------------------------+--------------+-----------
  12. ----+--------------+----------+----------+-------------+----------------+-------------+---------------+-------------+----
  13. -----+--------+---------------------+---------------------+-----------------------+-------------+-------+------------+----
  14. -------+----------------+---------------+

  15. | 1 | 1 | 2004-01-17 18:17:37 | 2004-01-17 16:17:37 | We'll see what time brings ... | Hello world! | 1
  16. | | NULL | NULL | publish | closed | closed | | hello-world
  17. | | | 2004-01-17 18:17:37 | 2004-01-17 16:17:37 | | 0 | /?p=1
  18. | 0 | post | | 0 |

  19. +----+-------------+---------------------+---------------------+--------------------------------+--------------+-----------
  20. ----+--------------+----------+----------+-------------+----------------+-------------+---------------+-------------+----
  21. -----+--------+---------------------+---------------------+-----------------------+-------------+-------+------------+----
  22. -------+----------------+---------------+
  23. 1 row in set (0.00 sec)

There is actually a nice solution for this. By default, the command line client uses horizontal mode to display the results of MySQL queries. If you use \G instead of ; or \g at the end of the line, your query result will be displayed in vertical mode, which will look a lot better:

  1. mysql> SELECT * FROM posts LIMIT 1\G
  2. *************************** 1. row ***************************
  3. ID: 1
  4. post_author: 1
  5. post_date: 2004-01-17 18:17:37
  6. post_date_gmt: 2004-01-17 16:17:37
  7. post_content: We'll see what time brings ...
  8. post_title: Hello world!
  9. post_category: 1
  10. post_excerpt:
  11. post_lat: NULL
  12. post_lon: NULL
  13. post_status: publish
  14. comment_status: closed
  15. ping_status: closed
  16. post_password:
  17. post_name: hello-world
  18. to_ping:
  19. pinged:
  20. post_modified: 2004-01-17 18:17:37
  21. post_modified_gmt: 2004-01-17 16:17:37
  22. post_content_filtered:
  23. post_parent: 0
  24. guid: /?p=1
  25. menu_order: 0
  26. post_type: post
  27. post_mime_type:
  28. comment_count: 0
  29. 1 row in set (0.00 sec)

While vertical output uses a lot more vertical space than the horizontal output, the vertical output is a lot more readable. Now you can probably see what I used as example query. It is actually the first post from this blog. Yes, this blog just got 6 years old ;).

If you want your command line client to use vertical mode by default, you can add vertical to the client section ([client]) of your .my.cnf file. This would look something like this:

  1. [client]
  2. vertical