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

 2022-10-27    447  

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

mysql>select1intooutfile'/tmp/t1.txt'; 
QueryOK,1rowaffected(0.00sec) 
mysql>select1intooutfile'/tmp/t1.txt'; 
ERROR1086(HY000):File'/tmp/t1.txt'alreadyexists 

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

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

mysql>pagercat>/tmp/t1.txt 
PAGERsetto'cat>/tmp/t1.txt' 
mysql>select1;\!cat/tmp/t1.txt 
1rowinset(0.00sec) 
+---+ 
|1| 
+---+ 
|1| 
+---+ 

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

mysql>pagercat>/tmp/t01.txt 
PAGERsetto'cat>/tmp/t01.txt' 
mysql>select12345unionselect67890; 
2rowsinset(0.02sec) 
mysql>pagercat>/tmp/t02.txt 
PAGERsetto'cat>/tmp/t02.txt' 
mysql>select12345unionselect67891; 
2rowsinset(0.00sec) 
mysql>\!vimdiff-o/tmp/t0[12].txt 
2filestoedit 
+-------+ 
|12345| 
+-------+ 
|12345| 
|67890| 
+-------+ 
/tmp/t01.txt 
+-------+ 
|12345| 
+-------+ 
|12345| 
|67891| 
+------+ 
/tmp/t02.txt 

  •  标签:  
  • MySQL
  •  

原文链接:https://77isp.com/post/4282.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。