2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SQL 実行結果を見やすく出力する

Last updated at Posted at 2021-04-16

目的

  • SQLの実行結果をテーブル状ではなく縦に出力する方法をメモ的にまとめる

方法

  1. 実行するSQLの末尾の;の代わりに\Gをつける。

紹介

  1. 下記のようなデータが格納されたusersテーブルが存在したとする。

    mysql> select * from users;
    +------+---------+-----------+------------+------------+
    | id   | name    | user_flag | admin_flag | check_flag |
    +------+---------+-----------+------------+------------+
    |    1 | admin_1 |         0 |          1 |          1 |
    |    2 | admin_2 |         0 |          1 |          0 |
    |    4 | user_2  |         1 |          0 |          0 |
    +------+---------+-----------+------------+------------+
    
  2. 上記にselect句の実行結果を載せているが、カラムが少ないのでテーブル状でも見やすい。しかし、たくさんのカラムが存在していた場合、レコードの情報が折り返されたりして直感的にデータを確認しにくい。

  3. 実行しているSQLの末尾の;\Gに置き換えて実行する事により下記のように出力される。

    mysql> select * from users\G
    *************************** 1. row ***************************
            id: 1
          name: admin_1
     user_flag: 0
    admin_flag: 1
    check_flag: 1
    *************************** 2. row ***************************
            id: 2
          name: admin_2
     user_flag: 0
    admin_flag: 1
    check_flag: 0
    *************************** 3. row ***************************
            id: 4
          name: user_2
     user_flag: 1
    admin_flag: 0
    check_flag: 0
    3 rows in set (0.00 sec)
    
  4. 各レコードの情報が縦に出力され、場合によってはこちらのほうが見やすい時がありそう。通常の出力方法と併用して都度見やすい方法で使い分けていきたい。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?