Home  >  Article  >  Database  >  MySQL查询结果导出到文件的另类方法

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

WBOY
WBOYOriginal
2016-06-07 14:51:471431browse

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  

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn