如何在终端中最好地显示返回太多字段的 MySQL SELECT?

发布于 2024-07-21 22:26:12 字数 480 浏览 7 评论 0原文

我正在使用 PuTTY 运行:

mysql> SELECT * FROM sometable;

sometable 有很多字段,这会导致很多尝试在终端中显示的列。 这些字段会换行到下一行,因此很难将列标题与字段值对齐。

有哪些解决方案可以在终端中查看此类数据?

我没有也不想访问 phpMyAdmin - 或任何其他 GUI 界面。 我正在寻找诸如此类的命令行解决方案: 将 MySQL 查询结果保存到文本或 CVS 文件

I'm using PuTTY to run:

mysql> SELECT * FROM sometable;

sometable has many fields and this results in many columns trying to be displayed in the terminal. The fields wrap onto the next line so it is very hard to line up column titles with field values.

What solutions are there for viewing such data in terminal?

I don't have nor want access to phpMyAdmin - or any other GUI interfaces. I'm looking for command-line solutions such as this one: Save MySQL Query results into text or CVS file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

枫林﹌晚霞¤ 2024-07-28 22:26:12

使用 \G 代替 ; 终止查询。 例如:

SELECT * FROM sometable\G

此查询垂直显示行,如下所示:

*************************** 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
          ...

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
          ...
挽手叙旧 2024-07-28 22:26:12

您可能还会发现这很有用(仅限非 Windows):

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

这将通过 less 命令行工具传输输出,该工具 - 使用这些参数 - 将为您提供可以水平和垂直滚动的表格输出用光标键。

q 键离开此视图,这将退出 less 工具。

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

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

This will pipe the output 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.

眸中客 2024-07-28 22:26:12

使用mysqlego命令

来自mysqlhelp命令:

ego         (\G)发送命令到mysql服务器,垂直显示结果。

因此,通过将 \G 附加到 select 中,您可以获得非常干净的垂直输出:

mysql> SELECT * FROM sometable \G

使用分页器

您可以告诉 MySQL 使用 less > pager 及其 -S 选项可以截断宽行并提供可以使用箭头键滚动的输出:

mysql> pager less -S

因此,下次运行具有宽输出的命令时,MySQL 将允许您浏览使用 less 寻呼机的输出:

mysql> SELECT * FROM sometable;

如果您使用完寻呼机并想返回到 stdout 上的常规输出,请使用以下命令:

mysql> nopager

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
甜味拾荒者 2024-07-28 22:26:12

尝试启用垂直模式,使用 \G 来执行查询而不是 ;

mysql> SELECT * FROM sometable \G

您的结果将以垂直模式列出,因此每个列值将打印在单独的线。 输出会更窄,但显然更长。

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.

苏璃陌 2024-07-28 22:26:12

您可以使用 --table-t 选项,这将输出一组漂亮的结果

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

或其他一些方法来将查询传递给 mysql,例如:

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

输出:

+--------------+--------------+------+-----+---------+----------------+
| 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    |                |
+--------------+--------------+------+-----+---------+----------------+

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    |                |
+--------------+--------------+------+-----+---------+----------------+

为了补充我认为最好的答案,我还使用 less -SFX 但以不同的方式:我喜欢将其添加到我的 .my.cnf 文件中home 文件夹中,示例 cnf 文件如下所示:

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

这样做的好处是,仅当查询的输出实际上超过一页长时才使用 less,这里是所有标志的解释:

  • -S:单行,当行比屏幕宽时不要跳过行,而是允许向右滚动。
  • -F:如果一个屏幕就退出,如果内容不需要滚动则只需发送到标准输出。
  • -X:无初始化,禁用任何“较少”可能已配置为每次加载时输出的输出。

注意:在 .my.cnf 文件中,不要将 pager 命令放在 [client] 关键字下面; 虽然它可能与 mysql 配合得很好,但 mysqldump 会抱怨无法识别它。

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.

分开我的手 2024-07-28 22:26:12

默认寻呼机是 stdout。 标准输出有列限制,因此输出将被换行。 您可以将其他工具设置为寻呼机来格式化输出。 有两种方法。 一是限制列,二是在vim中处理。

第一种方法:

➜  ~  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>

输出不完整。 内容适合您的屏幕。

第二个:

在 .vimrc 中将 vim 模式设置为 nowrap

➜  ~  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
+-----------+------------+------------+-------------+-------------+----------
~
~
~

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
+-----------+------------+------------+-------------+-------------+----------
~
~
~
假装爱人 2024-07-28 22:26:12

如果您以交互方式使用 MySQL,则可以将分页器设置为使用 sed,如下所示:

$ 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"

如果您不使用 sed 作为分页器,则输出如下所示:

"blah":"blah","blah":"blah","blah":"blah"

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"
唐婉 2024-07-28 22:26:12

我写了 pspg - https://github.com/okbob/pspg

该分页器专为表格数据而设计,并且也支持 MySQL。

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;

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;
坚持沉默 2024-07-28 22:26:12

我相信腻子有您可以为窗口指定的最大列数。

对于 Windows,我个人使用 Windows PowerShell 并将屏幕缓冲区宽度设置得相当高。 列宽保持固定,您可以使用水平滚动条查看数据。 我现在也遇到了和你一样的问题。

编辑:对于必须通过 SSH 连接的远程主机,您可以使用 plink + Windows PowerShell 之类的东西

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

甜心小果奶 2024-07-28 22:26:12

您可以使用 tee 将查询结果写入文件:

tee somepath\filename.txt

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

tee somepath\filename.txt
爱本泡沫多脆弱 2024-07-28 22:26:12

使用 Windows 命令提示符,您可以根据需要查看列数来增加窗口的缓冲区大小。 这取决于表中的列数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文