MySQL 为什么 SELECT * FROM table_name\G 中的 \G?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2277014/
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
Why the \G in SELECT * FROM table_name\G?
提问by knorv
Terminating a MySQL query with \G
instead of ;
will cause MySQL to return the result set in vertical format, which is sometimes easier to read if the number of returned columns is large.
使用\G
而不是终止 MySQL 查询;
会导致 MySQL 以垂直格式返回结果集,如果返回的列数很大,有时会更容易阅读。
Example:
例子:
mysql> SELECT * FROM help_keyword LIMIT 3\G
*************************** 1. row ***************************
help_keyword_id: 0
name: JOIN
*************************** 2. row ***************************
help_keyword_id: 1
name: REPEAT
*************************** 3. row ***************************
help_keyword_id: 2
name: SERIALIZABLE
3 rows in set (0.00 sec)
My question asked out of pure curiosity: Is there any rationale behind choosing the character combination \G
?
我的问题纯粹是出于好奇而提出的:选择角色组合背后有什么理由\G
吗?
采纳答案by Nate C-K
My thoughts:
我的想法:
- It probably means "go".
- Postgresql also uses \g as a statement terminator. Postgresql is older than MySQL so it might have influenced them.
- 大概意思是“去”。
- Postgresql 也使用 \g 作为语句终止符。Postgresql 比 MySQL 更旧,所以它可能影响了它们。
But as I pointed out in my comment to Andriyev's post above, it's actually making the \G uppercase that causes the display to be laid out vertically. Lowercase \g doesn't have that effect, or if it does then the documentation doesn't mention it. (I don't have a MySQL install handy to try it out.)
但正如我在上面对 Andriyev 帖子的评论中指出的那样,它实际上是使 \G 大写,导致显示垂直布局。小写 \g 没有这种效果,或者如果有,那么文档就没有提到它。(我没有方便的 MySQL 安装来试用它。)
回答by Gavin Hymanson
Short Answer
The ubiquitous semicolon command terminator ;
is actually shorthand for the \g
command, which is in itself shorthand for the go
command. The go
command is used both historically and currently in other flavours of SQL to submit batches of commands to be compiled and / or interpretted by the server. The \G
command seems to inherit it's characteristic letter from \g
, and is capitalised to further indicate a modified behaviour, as described by...mysql> help
...
\g go Send command to mysql server.
\G ego Send command to mysql server, display result vertically.
...
简答
无处不在的分号命令终止符;
实际上是\g
命令的简写,它本身就是go
命令的简写。该go
命令在历史上和当前在其他风格的 SQL 中都被使用,以提交要由服务器编译和/或解释的成批命令。该\G
命令似乎从 继承了它的特征字母\g
,并大写以进一步表示修改后的行为,如...mysql> help
...
\g go Send command to mysql server.
\G ego Send command to mysql server, display result vertically.
...
Longer Answer( It should really be \E)
Entering help
at the mysql prompt lists all the possible mysql commands, including go
and ego
shown above. The ego
command acquires a prepended 'e' indicating that this form of the go
command also adopts a behaviour that would normally be imposed by invoking mysql with the similar switch mysql -E
更长的答案(它应该真的是\E)在 mysql 提示符下
输入help
列出了所有可能的 mysql 命令,包括go
和ego
上面显示的。该ego
命令获得一个前置的“e”,表明这种形式的go
命令也采用了通常通过使用类似开关调用 mysql 所施加的行为mysql -E
From man mysql...
...
--vertical, -E
Print query output rows vertically (one line per column value).
Without this option, you can specify vertical output for individual
statements by terminating them with \G.
...
So why use -E
as shorthand for --vertical
?... Because both V
, v
, and e
had already been assigned as switches to other invocation behaviours. The ego
command could just have easily used \E
as it's shortcut, but confusingly adopted a capitalised version of the \g
command.
Fromman mysql...
...
--vertical, -E
Print query output rows vertically (one line per column value).
Without this option, you can specify vertical output for individual
statements by terminating them with \G.
...
那么为什么-E
用作--vertical
?... 的简写,因为V
, v
, 和e
已经被指定为其他调用行为的开关。该ego
命令可以很容易地用作\E
快捷方式,但令人困惑地采用了该\g
命令的大写版本。
In summary...
--vertical>> -E>> ego>> \G...Tada !
总之......
--vertical>> -E>> ego>> \G...Tada !
回答by Daniel
It matches the Unix command 'ls' formatting options
它匹配 Unix 命令 'ls' 格式选项