How to handle null field when exporting MYSQL tables to CSV

前端 未结 4 1085
死守一世寂寞
死守一世寂寞 2021-02-18 21:36

Right now when I export MYSQL tables to CSV files, I\'m getting \\N for NULL fields in the database which is expected. If there a way to change the \\N output to just an empty s

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-18 21:57

    The hardway:-

    SELECT 'CU_CustomerID','CU_UserName','CU_Password','CU_Email','CU_Company','CU_Comment','CU_LastBasket','CON_FirstName','CON_MiddleI','CON_LastName','CON_Address1','CON_Address2','CON_City','CON_State','CON_Province','CON_Country','CON_Zip','CON_Phone1','CON_Phone2'
    UNION
    SELECT COALESCE(T1.CU_CustomerID,''),COALESCE(T1.CU_UserName,''),COALESCE(T1.CU_Password,''),COALESCE(T1.CU_Email,''),COALESCE(T1.CU_Company,''),COALESCE(T1.CU_Comment,''),COALESCE(T1.CU_ShopperPoints,''),COALESCE(T2.CON_FirstName,''),COALESCE(T2.CON_MiddleI,''),COALESCE(T2.CON_LastName,''),COALESCE(T2.CON_Address1,''),COALESCE(T2.CON_Address2,''),COALESCE(T2.CON_City,''),COALESCE(T2.CON_State,''),COALESCE(T2.CON_Province,''),COALESCE(T2.CON_Country,''),COALESCE(T2.CON_Zip,''),COALESCE(T2.CON_Phone1,''),COALESCE(T2.CON_Phone2,'')
    FROM CUSTOMERS AS T1
    INNER JOIN CONTACT AS T2 ON T1.CU_CustomerID = T2.CON_CustomerID
    WHERE T1.CU_CustomerID BETWEEN 0 AND 1000
    INTO OUTFILE  'customers.csv'
    FIELDS TERMINATED BY  ','
    ENCLOSED BY  '"'
    

    This will default all values to an empty string rather than NULL ('\N').

提交回复
热议问题