1.26 Suppressing Column Headings in Query Output1.26.1 ProblemYou don't want to include column headings in query output. 1.26.2 Solution
Turn column headings off with the appropriate command-line option. Normally this is
-N
or
--skip-column-
1.26.3 Discussion
Tab-delimited format is
% mysql -e "SELECT arms FROM limbs" cookbook summarize To create output that contains only data values, suppress the column header row with the -N (or --skip-column-names ) option: % mysql -N -e "SELECT arms FROM limbs" cookbook summarize -N and --skip-column-names were introduced in MySQL 3.22.20. For older versions, you can achieve the same effect by specifying the "silent" option ( -s or --silent ) twice: % mysql -ss -e "SELECT arms FROM limbs" cookbook summarize Under Unix, another alternative is to use tail to skip the first line: % mysql -e "SELECT arms FROM limbs" cookbook tail +2 summarize |
1.27 Numbering Query Output Lines1.27.1 Problem
You'd like the lines of a query result
1.27.2 SolutionPostprocess the output from mysql , or use a SQL variable. 1.27.3 DiscussionThe -N option can be useful in combination with cat -n when you want to number the output rows from a query under Unix:
%
mysql -N -e "SELECT thing, arms FROM limbs" cookbook cat -n
1 human 2
2 insect 0
3 squid 10
4 octopus 8
5 fish 0
6 centipede 0
7 table 0
8 armchair 2
9 phonograph 1
10 tripod 0
11 Peg Leg Pete 2
12 NULL
Another option is to use a SQL variable. Expressions involving
mysql>
SET @n = 0;
mysql>
SELECT @n := @n+1 AS rownum, thing, arms, legs FROM limbs;
+--------+--------------+------+------+
rownum thing arms legs
+--------+--------------+------+------+
1 human 2 2
2 insect 0 6
3 squid 10 0
4 octopus 8 0
5 fish 0 0
6 centipede 0 100
7 table 0 4
8 armchair 2 4
9 phonograph 1 0
10 tripod 0 3
11 Peg Leg Pete 2 1
12 space alien NULL NULL
+--------+--------------+------+------+
|
1.28 Making Long Output Lines More Readable1.28.1 ProblemThe output lines from a query are too long. They wrap around and make a mess of your screen. 1.28.2 SolutionUse vertical output format. 1.28.3 DiscussionSome queries generate output lines that are so long they take up more than one line on your terminal, which can make query results difficult to read. Here is an example that shows what excessively long query output lines might look like on your screen: [9]
mysql>
SHOW FULL COLUMNS FROM limbs;
+-------+-------------+------+-----+---------+-------+-------------------------
--------+
Field Type Null Key Default Extra Privileges
+-------+-------------+------+-----+---------+-------+-------------------------
--------+
thing varchar(20) YES NULL select,insert,update,ref
erences
legs int(11) YES NULL select,insert,update,ref
erences
arms int(11) YES NULL select,insert,update,ref
erences
+-------+-------------+------+-----+---------+-------+-------------------------
--------+
An alternative is to generate "vertical" output with each column value on a separate line. This is done by terminating a query with
\G
rather than with a
;
character or with
\g
. Here's what the result from the
mysql>
SHOW FULL COLUMNS FROM limbs\G
*************************** 1. row ***************************
Field: thing
Type: varchar(20)
Null: YES
Key:
Default: NULL
Extra:
Privileges: select,insert,update,references
*************************** 2. row ***************************
Field: legs
Type: int(11)
Null: YES
Key:
Default: NULL
Extra:
Privileges: select,insert,update,references
*************************** 3. row ***************************
Field: arms
Type: int(11)
Null: YES
Key:
Default: NULL
Extra:
Privileges: select,insert,update,references
To specify vertical output from the command line, use the -E (or --vertical ) option when you invoke mysql . This affects all queries issued during the session, something that can be useful when using mysql to execute a script. (If you write the statements in the SQL script file using the usual semicolon terminator, you can select normal or vertical output from the command line by selective use of -E .) |