MySQL中SELECT ... INTO的用法

本文详细介绍了MySQL中SELECT INTO语句的四种使用方式,包括如何将查询结果存储到变量、输出到文件以及导入导出数据表。通过具体实例演示了每种用法的应用场景及操作步骤。

MySQLSELECT... INTO的用法

官方文档表述:

1.SELECT... INTO var_list selects column values and stores them intovariables.

2.SELECT... INTO OUTFILE writes the selected rows to a file. Column andline terminators can be specified to produce a specific outputformat.

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

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

>第一种测试:

mysql>select * from dx_tt;

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

| id| name |

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

1 | lyn |

2 | moon |

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

2rows 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;

ERROR1172 (42000): Result consisted of more than one row

QueryOK, 1 row affected (0.00 sec)

mysql>select @x,@y;

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

|@x   |@y  |

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

|1   | lyn |

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

1 rowin set (0.00 sec)

>2种测试:

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

->fieldsterminatedby ','  --字段的结束符

->optionallyenclosedby '"' –-字符串的结束符

   -> linesterminatedby '\n' --行的结束符

   -> from dx_tt;

QueryOK, 2 rows affected (0.03 sec)

文件内容如下:

D:\mysql_dir>catdx_tt3.txt

1,"lyn"

2,"moon"

>3种测试:

mysql>show tables;

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

|Tables_in_mydb |

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

|dx_lob        |

|dx_t          |

|dx_tt         |

|dx_users      |

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

4rows in set (0.00 sec)

mysql>drop table dx_lob;

QueryOK, 0 rows affected (0.42 sec)

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

QueryOK, 0 rows affected (0.14 sec)

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

QueryOK, 1 row affected (0.08 sec)

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

QueryOK, 2 rows affected (0.06 sec)

Records:2 Duplicates: 0  Warnings:0

mysql>select * from dx_tt;

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

| id| name |

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

1 | lyn |

2 | moon |

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

2rows in set (0.00 sec)

 

mysql>select * from dx_lob;

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

| id| pig       |

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

1 | helloworld |

2 | 李德华         |

3 | 卖空杰克逊       |

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

3rows 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;

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

fieldsterminated by ','

optionallyenclose' at line 1

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

   -> from dx_lob;

ERROR1172 (42000): Result consisted of more than one row

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

官方解释:

If youuse INTO DUMPFILE instead of INTO OUTFILE, MySQL writes only onerow into the file, without any column or line termination andwithout performing any escape processing. This is useful if youwant to store a BLOB value ina file.

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

   -> from dx_lob limit 1;

QueryOK, 1 row affected (0.00 sec)

文件内容如下:

D:\mysql_dir>catdx_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>catdx_tt2.txt

id     name

     lyn

     moon

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值