Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more explains and examples for SELECT ... INTO OUTFILE statements #15104

Merged
merged 15 commits into from
Nov 28, 2023
Merged
59 changes: 59 additions & 0 deletions sql-statements/sql-statement-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ TableSampleOpt ::=

## Examples

### SELECT

```sql
mysql> CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, c1 INT NOT NULL);
Query OK, 0 rows affected (0.11 sec)
Expand Down Expand Up @@ -159,6 +161,63 @@ mysql> SELECT AVG(s_quantity), COUNT(s_quantity) FROM stock;

The above example uses data generated with `tiup bench tpcc prepare`. The first query shows the use of `TABLESAMPLE`.

### SELECT ... INTO OUTFILE

`SELECT ... INTO OUTFILE` is used to write the result of a query to a file. Field and line terminators can be specified to produce a specific output format. Common output formats are comma separated (CSV) and tab separated (TSV).

You can use the `Fields` and `Lines` parameters to specify how to handle the data format.
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
qw4990 marked this conversation as resolved.
Show resolved Hide resolved

- `FIELDS TERMINATED BY`: specifies the data delimiter.
- `FIELDS ENCLOSED BY`: specifies the enclosing character of the data.
- `LINES TERMINATED BY`: specifies the line terminator, if you want to end a line with a certain character.

Below are some examples, first create a table and prepare some data:
qiancai marked this conversation as resolved.
Show resolved Hide resolved

```sql
mysql> create table t (a int, b varchar(10), c decimal(10,2));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t values (1, 'a', 1.1), (2, 'b', 2.2), (3, 'c', 3.3);
Query OK, 3 rows affected (0.01 sec)
```
qw4990 marked this conversation as resolved.
Show resolved Hide resolved

qiancai marked this conversation as resolved.
Show resolved Hide resolved
Then here are some `SELECT ... INTO OUTFILE` statements and their results.
qw4990 marked this conversation as resolved.
Show resolved Hide resolved

qiancai marked this conversation as resolved.
Show resolved Hide resolved
```sql
mysql> SELECT * FROM t INTO OUTFILE '/tmp/tmp_file1';
Query OK, 3 rows affected (0.00 sec)
```

qw4990 marked this conversation as resolved.
Show resolved Hide resolved
```
1 a 1.10
2 b 2.20
3 c 3.30
```
qw4990 marked this conversation as resolved.
Show resolved Hide resolved

qiancai marked this conversation as resolved.
Show resolved Hide resolved
```sql
mysql> select * from t into outfile '/tmp/tmp_file2' fields terminated by ',' enclosed by '"';
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
Query OK, 3 rows affected (0.00 sec)
```

qiancai marked this conversation as resolved.
Show resolved Hide resolved
```
"1","a","1.10"
"2","b","2.20"
"3","c","3.30"
```

qiancai marked this conversation as resolved.
Show resolved Hide resolved
```sql
mysql> select * from t into outfile '/tmp/tmp_file3' fields terminated by ',' enclosed by '\'' lines terminated by '<<<\n';
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
Query OK, 3 rows affected (0.00 sec)
```

qiancai marked this conversation as resolved.
Show resolved Hide resolved
```
'1','a','1.10'<<<
'2','b','2.20'<<<
'3','c','3.30'<<<
```

Now `SELECT ... INTO @variable`, `SELECT ... INTO DUMPFILE` or any [external storage](https://docs.pingcap.com/tidb/stable/backup-and-restore-storages) like S3 or GCS is not supported.

## MySQL compatibility

- The syntax `SELECT ... INTO @variable` is not supported.
Expand Down