MySQL查询结果导出到文件的另类方法

WBOY
リリース: 2016-06-07 14:51:47
オリジナル
1431 人が閲覧しました

MySQL查询结果导出到文件的方法很多人都会,一般大家都会用 SELECT INTO OUTFIL将查询结果导出到文件,但是这种MySQL查询结果导出到文件方法不能覆盖或者添加到已经创建的文件。例如: mysqlselect1intooutfile'/tmp/t1.txt'; QueryOK,1rowaffected(0.00sec)

  MySQL查询结果导出到文件的方法很多人都会,一般大家都会用 “SELECT INTO OUTFIL”将查询结果导出到文件,但是这种MySQL查询结果导出到文件方法不能覆盖或者添加到已经创建的文件。例如:

  mysql> select 1 into outfile '/tmp/t1.txt';  
  Query OK, 1 row affected (0.00 sec)  
  mysql> select 1 into outfile '/tmp/t1.txt';  
  ERROR 1086 (HY000): File '/tmp/t1.txt' already exists  

  还可以使用另外一种方法:

  mysql> pager cat > /tmp/t1.txt  
  PAGER set to 'cat > /tmp/t1.txt'  
  mysql> select 1;\! cat /tmp/t1.txt  
  1 row in set (0.00 sec)  
  +---+  
  | 1 |  
  +---+  
  | 1 |   
  +---+  

  这样你能很方便的查询到2条sql的差异:

  mysql> pager cat > /tmp/t01.txt  
  PAGER set to 'cat > /tmp/t01.txt'  
  mysql> select 12345 union select 67890;  
  2 rows in set (0.02 sec)  
  mysql> pager cat > /tmp/t02.txt  
  PAGER set to 'cat > /tmp/t02.txt'  
  mysql> select 12345 union select 67891;  
  2 rows in set (0.00 sec)  
  mysql> \! vimdiff -o  /tmp/t0[12].txt  
  2 files to edit  
    +-------+  
    | 12345 |  
    +-------+  
    | 12345 |  
    | 67890 |                                                                                                                   
    +-------+                                                                                                                
  /tmp/t01.txt   
    +-------+  
    | 12345 |  
    +-------+  
    | 12345 |  
    | 67891 |                                                                                                                   
    +------+                                                                                                           
  /tmp/t02.txt  

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!