LoginSignup
0
1

More than 3 years have passed since last update.

SQLのデータをtsvファイルとして出力する

Posted at

環境

MySQL

やりたいこと

SQLで出力されるこんな感じのデータを

> select id,name,memo from table where hoge = "fuga";
+----+--------------------------+-----------+
| id | name                     | memo      |
+----+--------------------------+-----------+
|  1 | test1                    | せやで     |
|  2 | test2                    | ほんま     |
|  3 | test3                    | かなわんなぁ |
|  4 | test4                    | がんばりや   |
|  5 | test5                    | なんでや    |
|  6 | test6                    | ねん       |
|  7 | test7                    | ほんま     |
+----+--------------------------+-----------+

こんな感じに出力して保存したい。

  id name    memo
  1  test1   せやで     
  2  test2   ほんま     
  3  test3   かなわんなぁ 
  4  test4   がんばりや   
  5  test5   なんでや    
  6  test6   ねん       
  7  test7   ほんま   

コード

$ echo "<出力したいデータを出すためのSQL文>" | mysql -u root | tee <出力先ファイル名>
  id name    memo
  1  test1   せやで     
  2  test2   ほんま     
  3  test3   かなわんなぁ 
  4  test4   がんばりや   
  5  test5   なんでや    
  6  test6   ねん       
  7  test7   ほんま 

$ cat <出力先ファイル名>
  id name    memo
  1  test1   せやで     
  2  test2   ほんま     
  3  test3   かなわんなぁ 
  4  test4   がんばりや   
  5  test5   なんでや    
  6  test6   ねん       
  7  test7   ほんま 
0
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
0
1