如何在终端中最好地显示返回太多字段的 MySQL SELECT?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用
\G
代替;
终止查询。 例如:此查询垂直显示行,如下所示:
Terminate the query with
\G
in place of;
. For example:This query displays the rows vertically, like this:
您可能还会发现这很有用(仅限非 Windows):
这将通过
less
命令行工具传输输出,该工具 - 使用这些参数 - 将为您提供可以水平和垂直滚动的表格输出用光标键。按
q
键离开此视图,这将退出less
工具。You might also find this useful (non-Windows only):
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 theless
tool.使用
mysql
的ego
命令来自
mysql
的help
命令:因此,通过将
\G
附加到select
中,您可以获得非常干净的垂直输出:使用分页器
您可以告诉 MySQL 使用
less
> pager 及其-S
选项可以截断宽行并提供可以使用箭头键滚动的输出:因此,下次运行具有宽输出的命令时,MySQL 将允许您浏览使用
less
寻呼机的输出:如果您使用完寻呼机并想返回到
stdout
上的常规输出,请使用以下命令:Using
mysql
'sego
commandFrom
mysql
'shelp
command:So by appending a
\G
to yourselect
, you can get a very clean vertical output: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:Thus, next time you run a command with a wide output, MySQL will let you browse the output with the
less
pager:If you're done with the pager and want to go back to the regular output on
stdout
, use this:尝试启用垂直模式,使用
\G
来执行查询而不是;
:您的结果将以垂直模式列出,因此每个列值将打印在单独的线。 输出会更窄,但显然更长。
Try enabling vertical mode, using
\G
to execute the query instead of;
: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.
您可以使用
--table
或-t
选项,这将输出一组漂亮的结果或其他一些方法来将查询传递给 mysql,例如:
输出:
You can use the
--table
or-t
option, which will output a nice looking set of resultsor some other method to pass a query to mysql, like:
output:
为了补充我认为最好的答案,我还使用
less -SFX
但以不同的方式:我喜欢将其添加到我的.my.cnf
文件中home 文件夹中,示例 cnf 文件如下所示:这样做的好处是,仅当查询的输出实际上超过一页长时才使用
less
,这里是所有标志的解释:注意:在
.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: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:Note: in the
.my.cnf
file don't put thepager
command below the[client]
keyword; although it might work withmysql
well,mysqldump
will complain about not recognizing it.默认寻呼机是 stdout。 标准输出有列限制,因此输出将被换行。 您可以将其他工具设置为寻呼机来格式化输出。 有两种方法。 一是限制列,二是在vim中处理。
第一种方法:
输出不完整。 内容适合您的屏幕。
第二个:
在 .vimrc 中将 vim 模式设置为 nowrap
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:
The output is not complete. The content fits to your screen.
The second one:
Set vim mode to nowrap in your .vimrc
如果您以交互方式使用 MySQL,则可以将分页器设置为使用
sed
,如下所示:如果您不使用
sed
作为分页器,则输出如下所示:If you are using MySQL interactively, you can set your pager to use
sed
like this:If you don't use
sed
as the pager, the output is like this:我写了
pspg
- https://github.com/okbob/pspg该分页器专为表格数据而设计,并且也支持 MySQL。
I wrote
pspg
- https://github.com/okbob/pspgThis pager is designed for tabular data - and MySQL is supported too.
我相信腻子有您可以为窗口指定的最大列数。
对于 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
您可以使用
tee
将查询结果写入文件:You can use
tee
to write the result of your query to a file:使用 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.