Export data from a table to a CSV file in MariaDB

MariaDb

We already have an example of saving table data to JSON: https://alexrait.blogspot.com/2023/01/export-data-from-table-to-json-file-in.html, now we do the same with CSV. The CSV should also have headers:



SELECT * 

INTO OUTFILE 'c:/path/to/output.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'

FROM (
	SELECT 'Field 1', 'Field 2', 'Field 3' 
    	UNION ALL (
        	SELECT	IFNULL(mt.Field1, ''), IFNULL(mt.Field2, ''), IFNULL(mt.Field3, '')
            FROM MyTable mt
            )
	)


Post a Comment

Previous Post Next Post