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

后端 未结 12 869
你的背包
你的背包 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 04:50

    I believe putty has a maximum number of columns you can specify for the window.

    For Windows I personally use Windows PowerShell and set the screen buffer width reasonably high. The column width remains fixed and you can use a horizontal scroll bar to see the data. I had the same problem you're having now.

    edit: For remote hosts that you have to SSH into you would use something like plink + Windows PowerShell

    0 讨论(0)
  • 2020-12-04 04:51

    The default pager is stdout. The stdout has the column limitation, so the output would be wrapped. You could set other tools as pager to format the output. There are two methods. One is to limit the column, the other is to processed it in vim.

    The first method:

    ➜  ~  echo $COLUMNS
    179
    
    mysql> nopager
    PAGER set to stdout
    mysql> pager cut -c -179
    PAGER set to 'cut -c -179'
    mysql> select * from db;
    +-----------+------------+------------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-
    | Host      | Db         | User       | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Grant_priv | References_priv | Index_priv | Alter_priv |
    +-----------+------------+------------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-
    | %         | test       |            | Y           | Y           | Y           | Y           | Y           | Y         | N          | Y               | Y          | Y          |
    | %         | test\_%    |            | Y           | Y           | Y           | Y           | Y           | Y         | N          | Y               | Y          | Y          |
    | localhost | phpmyadmin | phpmyadmin | Y           | Y           | Y           | Y           | Y           | Y         | N          | Y               | Y          | Y          |
    | localhost | it         | it         | Y           | Y           | Y           | Y           | Y           | Y         | N          | Y               | Y          | Y          |
    +-----------+------------+------------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-
    4 rows in set (0.00 sec)
    
    mysql>
    

    The output is not complete. The content fits to your screen.

    The second one:

    Set vim mode to nowrap in your .vimrc

    ➜  ~  tail ~/.vimrc
    
    " no-wrap for myslq cli
    set nowrap
    
    mysql> pager vim -
    PAGER set to 'vim -'
    mysql> select * from db;
        Vim: Reading from stdin...
    +-----------+------------+------------+-------------+-------------+----------
    | Host      | Db         | User       | Select_priv | Insert_priv | Update_pr
    +-----------+------------+------------+-------------+-------------+----------
    | %         | test       |            | Y           | Y           | Y
    | %         | test\_%    |            | Y           | Y           | Y
    | localhost | phpmyadmin | phpmyadmin | Y           | Y           | Y
    | localhost | it         | it         | Y           | Y           | Y
    +-----------+------------+------------+-------------+-------------+----------
    ~
    ~
    ~
    
    0 讨论(0)
  • 2020-12-04 04:54

    I wrote pspg - https://github.com/okbob/pspg

    This pager is designed for tabular data - and MySQL is supported too.

    MariaDB [sakila]> pager pspg -s 14 -X --force-uniborder --quit-if-one-screen
    PAGER set to 'pspg -s 14 -X --force-uniborder --quit-if-one-screen'
    MariaDB [sakila]> select now();
    MariaDB [sakila]> select * from nicer_but_slower_film_list limit 100;
    
    0 讨论(0)
  • 2020-12-04 04:56

    You can use tee to write the result of your query to a file:

    tee somepath\filename.txt
    
    0 讨论(0)
  • 2020-12-04 05:04

    Terminate the query with \G in place of ;. For example:

    SELECT * FROM sometable\G
    

    This query displays the rows vertically, like this:

    *************************** 1. row ***************************
                     Host: localhost
                       Db: mydatabase1
                     User: myuser1
              Select_priv: Y
              Insert_priv: Y
              Update_priv: Y
              ...
    *************************** 2. row ***************************
                     Host: localhost
                       Db: mydatabase2
                     User: myuser2
              Select_priv: Y
              Insert_priv: Y
              Update_priv: Y
              ...
    
    0 讨论(0)
  • 2020-12-04 05:04

    You might also find this useful (non-Windows only):

    mysql> pager less -SFX
    mysql> SELECT * FROM sometable;
    

    This will pipe the outut through the less command line tool which - with these parameters - will give you a tabular output that can be scrolled horizontally and vertically with the cursor keys.

    Leave this view by hitting the q key, which will quit the less tool.

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