- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we export data to a CSV file along with columns heading as its first line?
For adding the column values we need to use UNION statement. It can be demonstrated with the help of the following example −
In this example data from student_info will be exporting to CSV file. The CSV file will have the first line as the name of the columns.
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'); Query OK, 7 rows affected (0.04 sec)
After executing the above query MySQL creates Student_25.CSV file which have the following values −
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"
- How can we export all the data from MySQL table into a CSV file?
- How can we export data to a CSV file whose filename name contains timestamp at which the file is created?
- How can we export some field(s) from MySQL table into a CSV file?
- How can we store any other value than N in CSV file if we export the data to CSV file from a table which contains a NULL value(s)?
- Python Tkinter – How to export data from Entry Fields to a CSV file?
- How can we import data from .CSV file into MySQL table?
- How can we export all the data from MySQL table into a text file?
- How MySQL evaluates if we export the data to CSV file from a table which contains a NULL value(s)?
- How to Export and import CSV file manually in PowerShell?
- How can we import data from a text file having names of the columns in first row?
- Plot data from CSV file with Matplotlib
- How to save a vector in R as CSV file?
- How to save a matrix as CSV file using R?
- How can I clone/duplicate the table along with its data, trigger and indexes?
- How can we export some field(s) from MySQL table into a text file?