如何在 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 瀏覽

開始您的職業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.