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
MySQL查詢結果條數編號示例
MySQL查詢分頁的優化
MySQL查詢結果按某值排序
使用函數實現MySQL查詢行號
MySQL查詢中的非空問題