如果从包含 NULL 值的表中将数据导出到 CSV 文件,我们如何存储 CSV 文件中除了 N 之外的任何其他值?


如果我们希望在将数据从包含 NULL 值的表导出到 CSV 文件时,在 CSV 文件中存储 \N 以外的任何其他值,则需要使用 IFNULL 语句将 \N 值替换为其他值。为了说明这一点,我们以以下示例为例 -

示例

假设我们希望导出名为 'student_info' 的表的数值,该表具有以下数据 -

mysql> Select * from Student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Chandigarh | Literature |
| 125  | Raman   | Shimla     | Computers  |
| 130  | Ram     | Jhansi     | Computers  |
| 132  | Shyam   | Chandigarh | Economics  |
| 133  | Mohan   | Delhi      | Computers  |
| 150  | Saurabh | NULL       | Literature |
+------+---------+------------+------------+
7 rows in set (0.00 sec)

我们可以看到,对于 id 为 150 的地址字段,结果具有 NULL 值。现在,以下查询将把该表的数据导出到 Student_28.CSV 中,并在 \N 的位置存储 '不适用' -

mysql> Select IFNULL(id,'Not Applicable'), IFNULL(Name,'Not Applicable'), IFNULL(Address,'Not Applicable'), IFNULL(Subject,'Not Applicable') from Student_info INTO OUTFILE 'C:/mysql/bin/mysql-files/student_28.csv' FIELDS TERMINATED BY ',';
Query OK, 7 rows affected (0.02 sec)

我们可以看到,student_28.CSV 在 \N 的位置具有“不适用”,如下面的值所示 -

101   YashPal   Amritsar         History
105   Gaurav    Chandigarh       Literature
125   Raman     Shimla           Computers
130   Ram       Jhansi           Computers
132   Shyam     Chandigarh       Economics
133   Mohan     Delhi            Computers
150   Saurabh   Not Applicable   Literature

更新于: 2020年2月7日

173 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.