Purpose
You can use this statement to store the query results to a variable or file.
SELECT ... INTO OUTFILEstores the result set to an external file in the specified output format.SELECT ... INTO DUMPFILEwrites a single line without any format to an external file.SELECT ... INTO var_liststores the result set to variables.
Syntax
select_stmt INTO
OUTFILE string [ {CHARSET CHARACTER SET} charset_name] [field_opt] [line_opt]
DUMPFILE string
into_var_list
field_opt:
{COLUMNS FIELDS} field_term_list
line_opt:
LINES line_term_list
field_term_list:
field_term [, field_term ...]
field_term:
{[OPTIONALLY] ENCLOSED TERMINATED ESCAPED} BY string
line_term_list:
line_term [, line_term ...]
line_term:
{STARTING TERMINATED} BY string
Parameters
| Parameter | Description |
|---|---|
| OUTFILE DUMPFILE string | Stores the query results to the file specified by string. |
| CHARSET CHARACTER SET charset_name | Specifies the character set used to export the query results to an external file. |
| {COLUMNS FIELDS} field_term_list | Specifies the format of the field.
|
| LINES line_term_list | Specifies the format of the line.
|
Examples
Export the query results as a CSV file.
obclient> SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM test_table;Export the full join results of tables
AandBto an external file.obclient> SELECT distinct * FROM A FULL JOIN B USING(ID) INTO OUTFILE 'test.sql' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';