MySQLで枠線/列名を表示せずにSQLを実行する

  • 2
    Like
  • 0
    Comment
More than 1 year has passed since last update.

備忘録

通常

 mysql -u root --password=pw db_name -e "select id , created_at from user"
+----+---------------------+
| id | created_at          |
+----+---------------------+
|  1 | 2016-02-14 17:39:06 |
|  2 | 2016-02-14 17:39:28 |
|  3 | 2016-02-14 17:39:33 |
|  4 | 2016-02-14 17:39:38 |
|  5 | 2016-02-18 00:07:45 |
|  6 | 2016-04-27 23:09:30 |
|  8 | 2016-05-17 22:45:14 |
|  9 | 2016-05-28 18:33:53 |
| 10 | 2016-09-03 20:59:53 |
+----+---------------------+

枠線なし

-sオプションを付与するだけ

$ mysql -u root --password=pw db_name -e "select id , created_at from user" -s
id  created_at
1   2016-02-14 17:39:06
2   2016-02-14 17:39:28
3   2016-02-14 17:39:33
4   2016-02-14 17:39:38
5   2016-02-18 00:07:45
6   2016-04-27 23:09:30
8   2016-05-17 22:45:14
9   2016-05-28 18:33:53
10  2016-09-03 20:59:53

列名なし

-Nオプションを付与するだけ

$ mysql -u root --password=pw db_name -e "select id , created_at from user" -s -N
1   2016-02-14 17:39:06
2   2016-02-14 17:39:28
3   2016-02-14 17:39:33
4   2016-02-14 17:39:38
5   2016-02-18 00:07:45
6   2016-04-27 23:09:30
8   2016-05-17 22:45:14
9   2016-05-28 18:33:53
10  2016-09-03 20:59:53

使用例

ユーザのIDリストをDBから取得し、それに対して何らかの処理を行うスクリプトに標準入力で引き渡す

$ mysql -u root --password=pw db_name -e "select id from user" -s -N | script