Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the SQL query result without the table format

Tags:

shell

sql

mysql

Like the --disable-column-names option, do we have an option to get the SQL query without the table format? For example:

mysql -u username -p password  --disable-column-names --execute "select name from test" 

results below:

----- | A | | B | | C | | D | ----- 

Is it possible to get the query result using some sql program option modifiers as below, without the table format?

I want this:

A B C D 
like image 505
John Avatar asked May 23 '13 10:05

John


People also ask

How do I change the format of a SQL result?

I presume you are trying to change the result view in SQL Server Management Studio. If this the case what you need is 'Result to Grid' option. You can also use Ctrl + D to change the view to grid and 'Ctrl + T' to change it back to text.

What is %s in SQL query?

%s is a placeholder used in functions like sprintf. Check the manual for other possible placeholders. $sql = sprintf($sql, "Test"); This would replace %s with the string "Test".


2 Answers

Add the -B flag to mysql.

mysql -B -u username -ppassword \     --disable-column-names \     --execute "select name from mydb.test" 
-B, --batch: Print results in nontabular output format.  --execute: Execute the statement and quit. 

Note that -B/--batch also enables the --silent switch.

like image 98
Harsh Gupta Avatar answered Oct 19 '22 22:10

Harsh Gupta


Although the other answers work incidentally, the correct switch is actually -s which is short for --silent.

You may want to additionally specify -r for --raw output, which disables character escaping as well, otherwise newline, tab, null char and backslash will be represented as \n, \t, \0 and \ respectively.

   ·   --silent, -s         Silent mode. Produce less output. This option can be given multiple        times to produce less and less output.         This option results in nontabular output format and escaping of        special characters. Escaping may be disabled by using raw mode; see        the description for the --raw option.     ·   --raw, -r         For tabular output, the “boxing” around columns enables one column        value to be distinguished from another. For nontabular output (such        as is produced in batch mode or when the --batch or --silent option        is given), special characters are escaped in the output so they can        be identified easily. Newline, tab, NUL, and backslash are written        as \n, \t, \0, and \\. The --raw option disables this character        escaping.         The following example demonstrates tabular versus nontabular output        and the use of raw mode to disable escaping:             % mysql            mysql> SELECT CHAR(92);            +----------+            | CHAR(92) |            +----------+            | \        |            +----------+            % mysql -s            mysql> SELECT CHAR(92);            CHAR(92)            \\            % mysql -s -r            mysql> SELECT CHAR(92);            CHAR(92)            \ 

- Oracle Corporation

MySQL 5.7 06/07/2018

like image 42
Riot Avatar answered Oct 19 '22 22:10

Riot