Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

วิธีเก็บค่าอื่นแทน \N ในไฟล์ CSV เมื่อส่งออกข้อมูลที่มีค่า NULL

แทนที่ \N ด้วยค่าที่ต้องการโดยใช้ IFNULL

หากต้องการเก็บค่าอื่นแทน \N ในไฟล์ CSV เมื่อส่งออกข้อมูลจากตารางที่มีค่า NULL ให้ใช้คำสั่ง IFNULL เพื่อแทนที่ค่า NULL

ตัวอย่าง

ตาราง student_info มีข้อมูลดังนี้ (มีค่า NULL ที่ Address เมื่อ id=150):

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)

คำสั่งส่งออกพร้อมแทนที่ NULL ด้วย 'Not Applicable':

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:

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

ค่า NULL ถูกแทนที่ด้วย 'Not Applicable' แทน \N แล้ว