MySQL:使用 select into outfile 导出数据


#MySQL 笔记


示例:

// 建表,主键id为int类型
CREATE TABLE `test_table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL DEFAULT '' COMMENT '描述',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

// 插入数据
insert into test_table(name) values('name01');
insert into test_table(name) values('name02');

// 查看数据
select * from test_table;
+----+--------+
| id | name   |
+----+--------+
| 1  | name01 |
| 2  | name02 |
+----+--------+

// 导出数据
select * from test_table 
    into outfile '/tmp/test_table.csv' 
    fields terminated by ',' optionally enclosed by '"' 
    lines terminated by '\r\n';

查看导出文件的内容:

$ cat /tmp/test_table.csv
1,"name01"
2,"name02"

注意配置secure_file_priv以打开导入导出文件的特性,可以参考 MySQL:使用 load data 快速导入数据



( 本文完 )