How to store MySQL results to a file in table format
Asked Answered
B

3

9

I am trying to create a file and store in it the results from my query. I have a batch file that contains the single query,

USE database1;

SELECT * FROM table WHERE name = "abc" INTO OUTFILE output.txt;

QUIT

Executing this batch file using,

mysql -u root -p -t -vvv < select.sql

However, the result is not table formatted and fields' names are missing from the top.

100 abc Brown 32
101 abc Flair 25
102 abc McDonald 45
.
.
.

If I remove the INTO OUTFILE statement and print the results on terminal, then is working OK.

+----+------+---------+-----+
| id | name | surname | age |
+----+------+---------+-----+
| 100| abc  | Brown   |   32|
| 101| abc  | Flair   |   25|
| 102| abc  | McDonald|   45|
+----+------+---------+-----+

How can I achieve the above in a txt file?

UPDATE

Special thanks to GreyBeardedGeek. Here is the solution for this question with help of GreyBeardedGeek.

Batch file:

USE database1;

SELECT * FROM table WHERE name = "abc";

QUIT

and mysql client:

mysql -u root -p -t -vvv < select.sql > output.txt
Bissonnette answered 8/1, 2013 at 21:20 Comment(8)
remove the INTO outfile, and then do: mysql -u root -p -t -vvv < select.sql > output.txtAttorney
OK! Well...I lost my mind here! Is working, but why? What's the difference? Thank's a lot :)Bissonnette
INTO OUTFILE changes the default output, though it has options for quoting values, line delimiters, etc.Attorney
So, INTO OUTFILE, is useful if you want to store a csv file for example. And choose the delimiter symbols etc. I'm correct?Bissonnette
yes, it's usually used to get either CSV or TSV (Tab Delimited) outputAttorney
One more thing .. My output result has the sql query on top. There is any way to remove it? Any flag for this?Bissonnette
if you're on Linux, pipe the result through sed '1 d', e.g. mysql -u root -p -t -vvv < select.sql | sed '1 d' > output.txtAttorney
Either you or @Attorney should post this as an answer and then you should accept it.Leannaleanne
A
5

This should do the trick:

mysql -u root -p -t -vvv < select.sql | sed '1 d' > output.txt
Attorney answered 8/1, 2013 at 23:48 Comment(4)
What's the sed '1 d' part do?Collier
It removes the first line of the output, which in this case, is the sql queryAttorney
I'm guessing the -t option causes the output to be in a table format, but what does -vvv do?Heathenish
-vvv activates "verbose" modeAttorney
O
0

You can also do following:

mysql -uroot -p DatabaseName -t -e "select * from table" > file.txt

This doesn't need to make an SQL file and then filter out the first line.

Osmometer answered 2/9, 2018 at 1:6 Comment(0)
N
0

Besides using mysql client program options (mysql --help) one can configure defaults within .my.cnf file. Example (MacOs, Linux) to enable --table option:

edit/create a file:

vim ~/.my.cnf

add/update:

[client]
table

next time you call mysql command line program it will use options from .my.cnf file producing output in table format, e.g. the same as one has called:

mysql ... --table ... 

Please note that command line options override options from the .my.cnf files.

References:

Neuralgia answered 15/10, 2019 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.