如何在 MySQL 中导出特定列数据?


要导出 MySQL 中的特定列数据,请使用 OUTFILE −

select yourColumnName from yourTableName into outfile 'yourLocationOfFile’;

让我们首先创建一个表 −

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentFirstName varchar(20),
   StudentLastName varchar(20)
);
Query OK, 0 rows affected (0.54 sec)

使用 insert 命令向表中插入记录 −

mysql> insert into DemoTable(StudentFirstName,StudentLastName) values('John','Doe');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable(StudentFirstName,StudentLastName) values('David','Miller');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable(StudentFirstName,StudentLastName) values('John','Smith');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(StudentFirstName,StudentLastName) values('Carol','Taylor');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable(StudentFirstName,StudentLastName) values('Sam','Williams');
Query OK, 1 row affected (0.18 sec)

使用 select 命令从表中显示记录 −

mysql> select *from DemoTable;

这将产生以下输出 −

+-----------+------------------+-----------------+
| StudentId | StudentFirstName | StudentLastName |
+-----------+------------------+-----------------+
| 1         | John             | Doe             |
| 2         | David            | Miller          |
| 3         | John             | Smith           |
| 4         | Carol            | Taylor          |
| 5         | Sam              | Williams        |
+-----------+------------------+-----------------+
5 rows in set (0.00 sec)

以下是用于导出 MySQL 中特定列数据的查询 −

mysql> select StudentLastName from DemoTable into outfile 'E:\StudentLastName.txt';
Query OK, 5 rows affected (0.00 sec)

文件位置为“E:\StudentLastName.txt”。文件及其内容的屏幕截图如下。我们已成功将列数据导出到该文件 −

更新日期:30-Jul-2019

817 浏览

开始您的职业

完成课程即可获得认证

开始
广告