Search code examples
mysqlformattingbatch-processing

How to store MySQL results to a file in table format


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

Solution

  • This should do the trick:

    mysql -u root -p -t -vvv < select.sql | sed '1 d' > output.txt