A netizen asked in MySQL How to use only SQL statements to export files with a date format. Feel a little meaning, and then try it. The export file uses select INTO OUTFILE but the value behind OUTFILE cannot use variables, so it can only be implemented using dynamic SQL statements. Where table user is the test table. The specific statements are as follows
SET @SqlScript = CONCAT ("selectinto'/tmp/sql_out_", Date_format (Now (), '%y%m%d% h%i%s'), ". txt ' from 'user';");
rows Affected (0.01 sec)
PREPARE from @SqlScript;
rows Affected (0.00 sec)
Statement prepared
EXECUTE Sql_text;
rows Affected (0.00 sec)
There are several points to note about select INTO outfile, and you must ensure that the current account has write access to the corresponding directory. Otherwise, the permissions issue "can ' t create/write to file ' xxxxx ' (Errcode:13-permission denied)" will appear
In addition to the SQL syntax and knowledge points for MySQL prepare statements:
PREPARE from / * definition * /
Execute statement_name [USING/* Executes a preprocessing statement * /
{deallocatedropPREPARE/* Delete definition */;
The prepare statement is used to prepare a statement, specifying the name Statement_name, and then referencing the statement. The statement name is not case sensitive. The preparable_sql_statement can be a literal string or a user variable that contains the text of the statement. The text must appear as a single SQL statement, rather than as multiple statements. In this statement, '? ' A character can be used to identify a parameter when it is executed to indicate that the data value is bound to a query. ‘?’ Characters should not be quoted, even if you want to combine them with string values. Parameter markers can only be used where data values should appear, not SQL keywords, identifiers, etc.
If you can use tools such as Shell scripting, there are many ways to export data files with date formats as well. Do not start too much here.
How MySQL exports files with date formats