How does MySQL export files with date format and mysql export date format?

Source: Internet
Author: User

How does MySQL export files with date format and mysql export date format?

A netizen asked how to use only SQL statements in MySQL to export files with date format. I thought it was a bit interesting, so I tried it. The exported file uses select into outfile, but the values following OUTFILE cannot use variables, so they can only be implemented using dynamic SQL statements. The user table is the test table. The statement is as follows:

mysql> SET @SqlScript= CONCAT("SELECT * INTO OUTFILE '/tmp/sql_out_", DATE_FORMAT(NOW(), '%Y%m%d%H%i%s'), ".txt' FROM `user`;");
 
Query OK, 0 rows affected (0.01 sec)
 
mysql> PREPARE Sql_Text FROM @SqlScript;
 
Query OK, 0 rows affected (0.00 sec)
 
Statement prepared
 
mysql> EXECUTE Sql_Text;
 
Query OK, 33 rows affected (0.00 sec)
 
mysql> 

Note the following when selecting into outfile: Make sure that the current account has the write permission for the corresponding directory. Otherwise, a Permission error occurs. "Can't create/write to file 'xxxxx' (Errcode: 13-Permission denied )"

In addition, the SQL syntax and knowledge points of MySQL prepare statements are as follows:

PREPARE statement_name FROM preparable_ SQL _statement;/* definition */
 
EXECUTE statement_name [USING @ var_name [, @ var_name]...];/* EXECUTE the preprocessing statement */
 
{DEALLOCATE | DROP} PREPARE statement_name/* delete definition */;

The PREPARE statement is used to PREPARE a statement and specify the name statement_name. This statement will be referenced later. The statement name is case insensitive. Preparable_ SQL _statement can be a text string or a user variable containing the statement text. The text must be expressed as a single SQL statement, rather than multiple statements. In this statement ,'? The 'character can be used to identify a parameter. When executed, it indicates that the data value is bound to the query. '? 'Characters should not be enclosed in quotation marks, even if you want to combine them with string values. Parameter tags can only be used where data values should appear, rather than SQL keywords, identifiers, and so on.

If you can use tools such as shell scripts, there are still many ways to export data files with date format. Do not expand too much here.

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.