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

原创
2016-06-07 14:51:47 1111浏览

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。