How to best display in Terminal a MySQL SELECT returning too many fields?

后端 未结 12 889
你的背包
你的背包 2020-12-04 04:32

I\'m using PuTTY to run:

mysql> SELECT * FROM sometable;

sometable has many fields and this results in many columns trying

相关标签:
12条回答
  • 2020-12-04 05:04

    Using the Windows Command Prompt you can increase the buffer size of the window as much you want to see the number of columns. This depends on the no of columns in the table.

    0 讨论(0)
  • 2020-12-04 05:06

    Using mysql's ego command

    From mysql's help command:

    ego          (\G) Send command to mysql server, display result vertically.

    So by appending a \G to your select, you can get a very clean vertical output:

    mysql> SELECT * FROM sometable \G
    

    Using a pager

    You can tell MySQL to use the less pager with its -S option that chops wide lines and gives you an output that you can scroll with the arrow keys:

    mysql> pager less -S
    

    Thus, next time you run a command with a wide output, MySQL will let you browse the output with the less pager:

    mysql> SELECT * FROM sometable;
    

    If you're done with the pager and want to go back to the regular output on stdout, use this:

    mysql> nopager
    
    0 讨论(0)
  • 2020-12-04 05:09

    If you are using MySQL interactively, you can set your pager to use sed like this:

    $ mysql -u <user> p<password>
    mysql> pager sed 's/,/\n/g' 
    PAGER set to 'sed 's/,/\n/g''
    mysql> SELECT blah FROM blah WHERE blah = blah 
    .
    .
    .
    "blah":"blah"
    "blah":"blah"
    "blah":"blah"
    

    If you don't use sed as the pager, the output is like this:

    "blah":"blah","blah":"blah","blah":"blah"
    
    0 讨论(0)
  • 2020-12-04 05:12

    Try enabling vertical mode, using \G to execute the query instead of ;:

    mysql> SELECT * FROM sometable \G
    

    Your results will be listed in the vertical mode, so each column value will be printed on a separate line. The output will be narrower but obviously much longer.

    0 讨论(0)
  • 2020-12-04 05:14

    You can use the --table or -t option, which will output a nice looking set of results

    echo 'desc table_name' | mysql -uroot database -t
    

    or some other method to pass a query to mysql, like:

    mysql -uroot table_name --table < /tmp/somequery.sql
    

    output:

    +--------------+--------------+------+-----+---------+----------------+
    | Field        | Type         | Null | Key | Default | Extra          |
    +--------------+--------------+------+-----+---------+----------------+
    | id           | int(11)      | NO   | PRI | NULL    | auto_increment |
    | username     | varchar(30)  | NO   | UNI | NULL    |                |
    | first_name   | varchar(30)  | NO   |     | NULL    |                |
    | last_name    | varchar(30)  | NO   |     | NULL    |                |
    | email        | varchar(75)  | NO   |     | NULL    |                |
    | password     | varchar(128) | NO   |     | NULL    |                |
    | is_staff     | tinyint(1)   | NO   |     | NULL    |                |
    | is_active    | tinyint(1)   | NO   |     | NULL    |                |
    | is_superuser | tinyint(1)   | NO   |     | NULL    |                |
    | last_login   | datetime     | NO   |     | NULL    |                |
    | date_joined  | datetime     | NO   |     | NULL    |                |
    +--------------+--------------+------+-----+---------+----------------+
    
    0 讨论(0)
  • 2020-12-04 05:14

    Just to complement the answer that I thought best, I also use less -SFX but in a different way: I like to ad it to my .my.cnf file in my home folder, an example cnf file looks like this:

    [client]
    user=root
    password=MyPwD
    [mysql]
    pager='less -SFX'
    

    The good thing about having it this way, is that less is only used when the output of a query is actually more than one page long, here is the explanation of all the flags:

    • -S: Single line, don't skip line when line is wider than screen, instead allow to scroll to the right.
    • -F: Quit if one screen, if content doesn't need scrolling then just send to stdout.
    • -X: No init, disables any output "less" might have configured to output every time it loads.

    Note: in the .my.cnf file don't put the pager command below the [client] keyword; although it might work with mysql well, mysqldump will complain about not recognizing it.

    0 讨论(0)
提交回复
热议问题