How to export the result of mysql cmd to a text file?

Posted on 2006-11-29
Last Modified: 2013-11-18
Does anyone know how to export the result of mysql cmd to a text file?
Question by:kennysflau
  • 2
  • 2
LVL 19

Expert Comment

by:Kim Ryan
ID: 18043120
Use the -e option to run a command and quit, and redirect output to a text file.

mysql -u user -ppasswd -D dbase_name -e 'select * from table_name'  > report.txt
LVL 14

Accepted Solution

ygoutham earned 50 total points
ID: 18043457
there is a separate command in my sql called select into outfile

make sure that the file already does not exist before giving the command

SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
  FROM test_table;

in this instance the file "/tmp/result.txt" if it already exists then it gives a error.  it gives you a text file with , as the field separator to be imported into a excel, access, openoffice calc etc.
LVL 19

Expert Comment

by:Kim Ryan
ID: 18049784
This only works if you want a select statement. The -e option above will work with any statement, update, insert etc
LVL 14

Expert Comment

ID: 18051581
teraplane,  insert / update will only say how many number of rows were affected. more often users are looking for selecting values to a text file based on some suggestion.  and hence my suggestion.  

and  if the requirement is to run it as a scheduled task (in your instance you are presuming it to be alinux based system) whereas i only suggested a more generic sql syntax which is available as a pre-defined feature.

different ideas, no offence!!!

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
html input clean up 3 55
Problem to page 4 85
How can i make performance tuning to my sql query? 6 47
BACKUP of mysql database from mysql server - using Coldfusion 9 37
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to dynamically set the form action using jQuery.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question