MySQL中SELECT ... INTO的用法

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值