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

Does anyone know how to export the result of mysql cmd to a text file?
kennysflauAsked:
Who is Participating?
 
ygouthamConnect With a Mentor Commented:
there is a separate command in my sql called select into outfile

http://dev.mysql.com/doc/refman/5.0/en/select.html

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

*********
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  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.
0
 
Kim RyanIT ConsultantCommented:
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
0
 
Kim RyanIT ConsultantCommented:
This only works if you want a select statement. The -e option above will work with any statement, update, insert etc
0
 
ygouthamCommented:
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!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.