您的位置:首页 > 数据库 > MySQL

MySQL中SELECT ... INTO的用法

2015-02-12 11:10 381 查看
MySQL中SELECT ... INTO的用法

官方文档表述:

1.SELECT ... INTO var_list selects column values and stores them into variables.

2.SELECT ... INTO OUTFILE writes the selected rows to a file. Column and line terminators can be specified to produce a specific output format.

3.SELECT ... INTO DUMPFILE writes a single row to a file without any formatting.

4. mysql –e “SELECT .. FROM” >FILENAME

>第一种测试:

mysql> select * from dx_tt;

+----+------+

| id | name |

+----+------+

| 1 | lyn |

| 2 | moon |

+----+------+

2 rows in set (0.00 sec)

mysql> select id,name INTO @x,@y from dx_tt limit 1; --这里必须限制一行否则报如下错误:

mysql> select id,name INTO @x,@y from dx_tt;

ERROR 1172 (42000): Result consisted of more than one row

Query OK, 1 row affected (0.00 sec)

mysql> select @x,@y;

+------+------+

| @x | @y |

+------+------+

| 1 | lyn |

+------+------+

1 row in set (0.00 sec)

>第2种测试:

mysql> select id,name INTO OUTFILE 'D:\mysql_dir\\dx_tt3.txt'

-> fields terminated by ',' --字段的结束符

->optionally enclosed by '"' –-字符串的结束符

-> lines terminated by '\n' --行的结束符

-> from dx_tt;

Query OK, 2 rows affected (0.03 sec)

文件内容如下:

D:\mysql_dir>cat dx_tt3.txt

1,"lyn"

2,"moon"

>第3种测试:

mysql> show tables;

+----------------+

| Tables_in_mydb |

+----------------+

| dx_lob |

| dx_t |

| dx_tt |

| dx_users |

+----------------+

4 rows in set (0.00 sec)

mysql> drop table dx_lob;

Query OK, 0 rows affected (0.42 sec)

mysql> create table dx_lob(id int not null primary key,pig blob);

Query OK, 0 rows affected (0.14 sec)

mysql> insert into dx_lob values(1,'helloworld');

Query OK, 1 row affected (0.08 sec)

mysql> insert into dx_lob values(2,'李德华'),(3,'卖空杰克逊');

Query OK, 2 rows affected (0.06 sec)

Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from dx_tt;

+----+------+

| id | name |

+----+------+

| 1 | lyn |

| 2 | moon |

+----+------+

2 rows in set (0.00 sec)

mysql> select * from dx_lob;

+----+------------+

| id | pig |

+----+------------+

| 1 | helloworld |

| 2 | 李德华 |

| 3 | 卖空杰克逊 |

+----+------------+

3 rows in set (0.00 sec)

mysql> select pig INTO DUMPFILE 'D:\mysql_dir\\dx_lob1.dmp'

-> fields terminated by ','

-> optionally enclosed by '"'

-> lines terminated by '\n'

-> from dx_lob;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DUMPFILE 'D:\mysql_dir\\dx_lob1.dmp'

fields terminated by ','

optionally enclose' at line 1

mysql> select pig INTO DUMPFILE 'D:\mysql_dir\\dx_lob3.dmp'

-> from dx_lob;

ERROR 1172 (42000): Result consisted of more than one row

这也就说明INTO DUMPFILE写入文件是不带任何格式的,而且只能写入一行

官方解释:

If you use INTO DUMPFILE instead of INTO OUTFILE, MySQL writes only one row into the file, without any column or line termination and without performing any escape processing. This is useful if you want to store a BLOB value in a file.

mysql> select pig INTO DUMPFILE 'D:\mysql_dir\\dx_lob1.dmp'

-> from dx_lob limit 1;

Query OK, 1 row affected (0.00 sec)

文件内容如下:

D:\mysql_dir>cat dx_lob1.dmp

helloworld

>第4种测试:

将数据库mydb中的表dx_tt数据导入文件dx_tt2.txt

C:\>mysql -ulyn -p123456 mydb -e "select id,name from dx_tt" >D:\mysql_dir\dx_tt2.txt

内容如下:

D:\mysql_dir>cat dx_tt2.txt

id name

1 lyn

2 moon
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: