MySQL Linux 命令行工具和正确格式化的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19245653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:04:39  来源:igfitidea点击:

MySQL Linux command line tool and properly formatted results

mysqlselectcommand-line

提问by Peter Bushnell

Does anyone know of a good tool to use from the shell to query the database and get intelligible results?

有谁知道从 shell 中使用的一个很好的工具来查询数据库并获得可理解的结果

On the command line, from SSH, I want to query the database using mysqlbut the results are pretty much unreadable because there is no formatting for the terminal screen.

在命令行上,我想从 SSH 查询数据库,mysql但结果几乎无法读取,因为终端屏幕没有格式。

If I run the following it is vary hard to make sense of the results.

如果我运行以下命令,则很难理解结果。

use mydb;
select * from db3_settings;

I know I could use phpMyAdmin or the MySQLCC but I'm looking for a command-line solution.

我知道我可以使用 phpMyAdmin 或 MySQLCC,但我正在寻找命令行解决方案。

回答by jwbensley

You can obtain a vertically formatted output with \G.

您可以使用\G.

This is the standard output:

这是标准输出:

mysql> select * from tblSettings;
+-----------+----------------+---------------------+
| settingid | settingname    | settingvalue        |
+-----------+----------------+---------------------+
|         1 | maxttl         | 30                  |
|         2 | traceroutepath | /usr/bin/traceroute |
|         3 | alertemail     | [email protected]         |
+-----------+----------------+---------------------+
3 rows in set (0.00 sec)

And this is what the output looks like with \G:

这就是输出的样子\G

mysql> select * from tblSettings \G;
*************************** 1. row ***************************
   settingid: 1
 settingname: maxttl
settingvalue: 30
*************************** 2. row ***************************
   settingid: 2
 settingname: traceroutepath
settingvalue: /usr/bin/traceroute
*************************** 3. row ***************************
   settingid: 3
 settingname: alertemail
settingvalue: [email protected]
3 rows in set (0.00 sec)

回答by ModulusJoe

There are a number of ways you can make results more readable.

有多种方法可以使结果更具可读性。

Using limit to only view a few rows:

使用限制只查看几行:

SELECT * FROM table LIMIT 10;

Only returning select columns:

只返回选择列:

SELECT a,b,c FROM table;

Terminating your queries with \Gto send the current statement to the server to be executed and display the result using vertical format:

终止查询\G以将当前语句发送到要执行的服务器并使用垂直格式显示结果:

SELECT * FROM table \G

回答by karthik339

In command line, you can get have the results of the returned records shown as individual, formatted records (as opposed to a long table) by using \Gat the end of your query, like this:

在命令行中,您可以\G在查询结束时使用,将返回记录的结果显示为单独的格式化记录(而不是长表),如下所示:

select * from <tablename> \G;

回答by computingfreak

For non-Windows systems, you can use lessto make the output formatted or more readable.

对于非 Windows 系统,您可以使用less使输出格式化或更具可读性。

mysql > pager less -SFX;
mysql > select * from databasename.table;

it pipes the sql output to less giving a tabular output that can be scrolled horizontally and vertically with the cursor keys. exit by pressing 'q'

它通过管道将 sql 输出减少给一个表格输出,可以使用光标键水平和垂直滚动。按“q”退出

if you don't want, reset by using

如果您不想,请使用重置

mysql> nopager