An alternative method for exporting MySQL query results to files

Source: Internet
Author: User
Many users can export MySQL query results to files. Generally, SELECTINTOOUTFIL 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. Example: mysqlselect11_outfiletmpt1.txt; QueryOK, 1 rowaffected (0.00sec)

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. Example: mysqlselect11_outfile '/tmp/t1.txt'; QueryOK, 1 rowaffected (0.00sec)

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/t0127122.16.txt
2 files to edit
+ ------- +
| 1, 12345 |
+ ------- +
| 1, 12345 |
| 1, 67890 |
+ ------- +
/Tmp/t01.txt
+ ------- +
| 1, 12345 |
+ ------- +
| 1, 12345 |
| 1, 67891 |
+ ------ +
/Tmp/t02.txt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.