为了添加列值,我们需要使用 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"
以上是我们如何将数据导出到 CSV 文件,并将列标题作为第一行?的详细内容。更多信息请关注PHP中文网其他相关文章!