如果要在从包含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 ',';
从以下值可以看出,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