Run SQL query with CSV format

1143015 pts.
Tags:
CSV
Linux
SQL Query
I'm trying to run a SQL query from my Linux command line and getting the result in a CSV format. Here's what I have so far:
mysql -u uid -ppwd -D dbname << EOQ | sed -e 's/        /,/g' | tee list.csv
select id, concat(""",name,""") as name
from students
EOQ
And here's my issue: The columns that are surrounded by quotes makes the query difficult. What can I do here? Thank you for your help.
1

Answer Wiki

Thanks. We'll let you know when a new response is added.

You can export the results to a file in CSV format, this way:

SELECT id, name
FROM students
INTO OUTFILE '/tmp/students.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

The line in blue will add the quotes automatically.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: