Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7089895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:58:41+00:00 2026-05-28T07:58:41+00:00

Possible Duplicate: How to export / dump a MySql table into a text file

  • 0

Possible Duplicate:
How to export / dump a MySql table into a text file including the field names (aka headers or column names)

I use this SQL snippet to dump a table into CSV text files:

SELECT * FROM brand INTO OUTFILE “e:/brand.csv”
FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘
LINES TERMINATED BY “\n”;

However this approach doesn’t add the column names at the beginning of the CSV file. My question is how to select all the column / field names as well, just like what phpMyAdmin does when you export the table and select “Put fields names in the first row”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:58:42+00:00Added an answer on May 28, 2026 at 7:58 am

    I figured out a way around having to manually enter those names as long as you’re running MySQL 5 or higher. Here it is, written as a bash script for running on a unix command line:

    DBNAME=<database_name>
    TABLE=<table_name>
    
    FNAME=/path/to/output/dir/$(date +%Y.%m.%d)-$DBNAME.csv
    
    #(1)creates empty file and sets up column names using the information_schema
    mysql -u <username> -p<password> $DBNAME -B -e "SELECT COLUMN_NAME FROM information_schema.COLUMNS C WHERE table_name = '$TABLE';" | awk '{print $1}' | grep -iv ^COLUMN_NAME$ | sed 's/^/"/g;s/$/"/g' | tr '\n' ',' > $FNAME
    
    #(2)appends newline to mark beginning of data vs. column titles
    echo "" >> $FNAME
    
    #(3)dumps data from DB into /var/mysql/tempfile.csv
    mysql -u <username> -p<password> $DBNAME -B -e "SELECT * INTO OUTFILE '/var/mysql/tempfile.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM $TABLE;"
    
    #(4)merges data file and file w/ column names
    cat /var/mysql/tempfile.csv >> $FNAME
    
    #(5)deletes tempfile
    rm -rf /var/mysql/tempfile.csv
    

    While not the most graceful solution, i’m sure it can be compressed into a single line by someone who knows SQL and/or bash a little better than me…

    What it does is:

    1. uses MySQL’s information schema to create an empty CSV w/ column headers
    2. appends an extra newline to that empty CSV so your data will begin appearing a new line
    3. uses a pretty standard “SELECT * INTO OUTFILE…” query to create a CSV full of data
    4. appends the data file onto the file w/ column headers
    5. deletes the (temporary) data file

    Good luck, and if you clean it up, post your results!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: When is it OK to use an XML file to save information?
Is it possible to duplicate an entire MySQL database on a Linux server ?
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: Using SSL in an iPhone App - Export Compliance I am in
Possible Duplicate: Ignore SVN files when exporting a WAR file from Eclipse? Using eclipse
Possible Duplicate: In Vim: How do I delete a word and go into insert
Possible Duplicate: git - removing a file from source control (but not from the
Possible Duplicate: Dealing with commas in a CSV file We are exporting a bulk
Possible Duplicate: How can I convert my java program to an .exe file ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.