MySQL exports the specified data method by query criteria

Source: Internet
Author: User

To conditionally export data for MySQL tables:

The code is as follows Copy Code

Theyestoday= ' date-d '-1 day "+%y%m%d" #前天日期

#将查询语句写入sql文件
Export in Linux

The code is as follows Copy Code
echo "Select NULL, Coder, Clicknum, time in stat.yl_clicks WHERE time = Unix_timestamp (' $theyestoday 2 00:00:00′) into OutFile '/var/lib/mysql/stat/yl_clicks.sql '; "> Cmd_yl_clicks.sql

Window System Export

The code is as follows Copy Code

SELECT Item1_1,id,item2_1,item11_1,item9_1,item7_1,item8_1,item4_1,item4_2,item6_1,item6_2,item5_1,item5_2, Item5_3,item5_4,item5_5,item5_6,item5_7 from ' Id_7_vote_insert ' WHERE ' voteid ' = ' 7 ' ORDER by item1_1 ASC into OutFile '/TM P/test_new.txt ' fields terminated by ', ' enclosed by ' ';

#删除上次生成的

The code is as follows Copy Code

Rm-rf/var/lib/mysql/stat/yl_clicks.sql

#执行sql文件中的语句

The code is as follows Copy Code

Mysql-u User name-p password-hlocalhost < cmd_yl_clicks.sql

#执行完成后将生成查下结果数据文件 Yl_clicks.sql


Import Data


#导入数据并生成导入日志 (the record of the imported data, such as how many records are imported)

The code is as follows Copy Code

Mysqlimport-u User name-p password-f-i stat "d:systemmysqlmysql Server 5.0datastatyl_clicks.sql" >> Synchronization2.log

Summarize:

String must be handled well, otherwise it is prone to error.
Use the SELECT INTO outfile the "filename" statement if you have permission to the file.
Like what:
Database is MySQL

The code is as follows Copy Code
Mysql> SELECT * from user into outfile "a.txt";

The above can be done to unload the table user from the database into the A.txt file. Note that the filename should be enclosed in single quotes. Then the a.txt file has a data directory in the MySQL directory, which is where the database files are placed. Here is MySQL. There will be a a.txt file underneath. A.txt's content is:
localhost root *6f7418838fc8b9085d8b2e7484d471e34710f635 y y y-y, y-y y-y-y. Y y, y-y, y-axis y, y-y y y 0 0 0 0

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.