如何将数据按照其第一列是标题的方式导出到 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"
广告