如何在 MySQL 的命令行中显示变量的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18838611/
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 18:52:20  来源:igfitidea点击:

How to display the value of a variable at the commandline in MySQL?

mysqlvariablescommand-line

提问by CodeBlue

I tried the following -

我尝试了以下 -

I created a variable at the command prompt as follows -

我在命令提示符下创建了一个变量,如下所示 -

mysql> set @myId = 1;
Query OK, 0 rows affected (0.00 sec)

Then, to display it, I tried the following without success -

然后,为了显示它,我尝试了以下但没有成功 -

    mysql> show myId;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near 'myId' at line 1
    mysql> show @myId;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '@myId' at line 1
    mysql> PRINT @myId;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near 'PRINT @myId' at line 1
    mysql> PRINT myId;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near 'PRINT myId' at line 1

So how can I display the value of @myId?

那么如何显示 的值@myId呢?

回答by Mike Brant

Simply SELECTthe variable like this:

简单SELECT的变量是这样的:

SELECT @myId;

Here is the MySQL documentation on user-defined variables:

这是有关用户定义变量的 MySQL 文档:

http://dev.mysql.com/doc/refman/5.5/en/user-variables.html

http://dev.mysql.com/doc/refman/5.5/en/user-variables.html

回答by Ryan Shillington

If you're looking for a variable that you set yourself like the OP did, then @MikeBrant's answer is correct:

如果您正在寻找像 OP 一样为自己设置的变量,那么@MikeBrant 的答案是正确的:

SELECT @myId;

But if you want to see the MySQL system variables (which is what I came here looking for), then you need to run:

但是如果你想查看 MySQL 系统变量(这是我来这里寻找的),那么你需要运行:

show variables like '%slow%';

or possibly:

或者可能:

show global variables like '%slow%';

回答by Wilson Hauck

SHOW GLOBAL STATUS LIKE '%com_stmt%'; may be used to determine any SHOW GLOBAL STATUS current values using wildcard.

像'%com_stmt%'一样显示全局状态;可用于使用通配符确定任何 SHOW GLOBAL STATUS 当前值。

Likewise, SELECT @@thread_cache_size; may be used to display any specific SHOW GLOBAL VARIABLES current value.

同样,SELECT @@thread_cache_size; 可用于显示任何特定的 SHOW GLOBAL VARIABLES 当前值。

There are more than 300 GLOBAL STATUS values.

有超过 300 个 GLOBAL STATUS 值。

There are more than 400 GLOBAL VARIABLES with or without values. (Could be empty placeholders).

有 400 多个带或不带值的全局变量。(可能是空的占位符)。

You CAN NOT create a GLOBAL VARIABLE in MySQL.

您不能在 MySQL 中创建全局变量。