如果從包含 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
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP