Thursday, March 12, 2009

MySQL: controlling output display with line terminators

When using the mysql command line interface, you can specify whether to display your results horizontally or vertically by using different line terminators. Using a ";" or "\g" causes the output to be displayed horizontally while a "\G" will cause the display to be vertical. As an example consider the following. Here we use the "\g" to display output horizontally. A ";" will yield the same results:

mysql> select id, name from sometable\g
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | Geddy Lee |
| 2 | Neil Peart |
| 3 | Alex Lifeson |
+----+-------------------+
3 rows in set (0.00 sec)


Using the same query with the "\G" terminator:

mysql> select id, name from sometable\G
*************************** 1. row ***************************
id: 1
name: Geddy Lee
*************************** 2. row ***************************
id: 2
name: Neil Peart
*************************** 3. row ***************************
id: 3
name: Alex Lifeson
3 rows in set (0.00 sec)


For output with many columns, the second option is often preferred as it is more readable.

No comments:

Post a Comment