151

I'm executing some SQL statements in batch (using the mysql command-line binary). I want one of my several SELECT statements to not print the column headers, just the selected records. Is this possible?

3 Answers 3

319

Invoke mysql with the -N (the alias for -N is --skip-column-names) option:

mysql -N ...
use testdb;
select * from names;

+------+-------+
|    1 | pete  |
|    2 | john  |
|    3 | mike  |
+------+-------+
3 rows in set (0.00 sec)

Credit to ErichBSchulz for pointing out the -N alias.

To remove the grid (the vertical and horizontal lines) around the results use -s (--silent). Columns are separated with a TAB character.

mysql -s ...
use testdb;
select * from names;

id  name
1   pete
2   john
3   mike

To output the data with no headers and no grid just use both -s and -N.

mysql -sN ...
4
  • 5
    -sN worked well for me to assign the output to a variable in a script: TABLES=$(mysql -sN -u $DB_USER -p$DB_PASS...
    – Michael J
    Apr 28, 2016 at 20:23
  • 8
    This applies to the entire session, not just to a single SQL statement. Oracle SQLPlus has set feedback on and set feedback off which can be used anywhere in a session. Does MySQL have an equivalent? Looks like that's what OP was looking for. Mar 13, 2018 at 17:24
  • just a brief comment, simplify using select * from testdb.names; without explicit 'use'.
    – fcm
    Mar 25, 2019 at 12:02
  • 4
    The long option for -s is --silent, for -N --skip-column-names. -B or --batch also works well instead of -s.
    – Ale
    Jan 20, 2021 at 9:00
21

You can fake it like this:

-- with column headings 
select column1, column2 from some_table;

-- without column headings
select column1 as '', column2 as '' from some_table;
3
  • Error: Type mismatch: expected type string, but got error with empty alias
    – QkiZ
    Jun 4, 2020 at 13:16
  • Looks like that error is coming from MySQL Workbench, not from MySQL. Anyway you can also use a single blank space instead of an empty string, and that seems to work in MySQL Workbench: select column1 as ' ', column2 as ' ' from some_table; Jun 7, 2020 at 14:22
  • this approach leaves an initial blank line that may need to be removed. Jun 12, 2022 at 1:11
0

A good reason to "... want ... SELECT statements to not print the column headers..." is for documenting output.

Thanks to @tom_warfield I do this:

select "Relevant details from Stock Entry." as ""\G
select 
      SE.name
    , SED.item_code
    , SED.s_warehouse
    , SED.t_warehouse
    , REPLACE(SED.serial_no,'\n',', ') as serial_no
from
    `tabStock Entry` SE left join `tabStock Entry Detail` SED 
    :
    :

My output then looks like this:

*************************** 1. row ***************************
: Relevant details from Stock Entry.
+--------------------+-------------------------------+--------------------------+----------------------------+---------------------------------------------------------------------------------------------------------------------+
| name               | item_code                     | s_warehouse              | t_warehouse                | serial_no                                                                                                           |

Note that "\G" instead of ";" outputs one attribute per line, rather than one row per line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.