Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Thay thế \N bằng giá trị tùy chỉnh khi xuất CSV chứa NULL

Vấn đề: MySQL ghi \N cho NULL

Khi xuất ra CSV, MySQL mặc định ghi ký tự \N cho giá trị NULL. Để thay bằng văn bản khác (ví dụ "Not Applicable"), dùng hàm IFNULL() trong câu SELECT.

Ví dụ

Bảng Student_info có hàng id=150 với Address=NULL:

mysql> SELECT * FROM Student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 150  | Saurabh | NULL       | Literature |
+------+---------+------------+------------+

Xuất với IFNULL:

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 ',';

Kết quả trong file CSV:

150 Saurabh Not Applicable Literature