Many users can export MySQL query results to files. Generally, "select into outfil" is used to export query results to files, however, the method for exporting the MySQL query results to a file cannot overwrite or add the results to the created file. For example:
- 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
You can also use another method:
- 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 |
- +---+
In this way, you can easily query the differences between two SQL statements:
- 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
Example of the number of MySQL query results
Optimization of MySQL query Paging
MySQL query results are sorted by a certain value
Use functions to query row numbers in MySQL
Non-empty question in MySQL Query