如何导出数据,及其作为第一行的列标题到 CSV 文件中?
要添加列值,我们需要使用 UNION 语句。这可以通过以下示例进行说明:
示例
此示例中的数据将在 student_info 中导出到 CSV 文件。CSV 文件的第一行将作为列的名称。
mysql>(SELECT 'id', 'Name', 'Address', 'Subject')UNION(SELECT id, Name, Address, Subject From student_info INTO OUTFILE 'C:/mysql/bin/mysql-files/student_25.CSV' FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY '\r
'); Query OK, 7 rows affected (0.04 sec)
执行上述查询后,MySQL 会创建 Student_25.CSV 文件,其中包含以下值:
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"
广告