我们如何将数据以及列标题作为第一行导出到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\n');
执行完上面的查询后,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"