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

Cod*_*lue 41 mysql variables command-line

我试过以下 -

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

mysql> set @myId = 1;
Query OK, 0 rows affected (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

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

    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
Run Code Online (Sandbox Code Playgroud)

那么我怎样才能显示出价值@myId

Mik*_*ant 63

只需SELECT像这样的变量:

SELECT @myId;
Run Code Online (Sandbox Code Playgroud)

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

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

  • 好的,但知道要搜索什么是什么让你在26k和我在2k. (17认同)

Rya*_*ton 10

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

SELECT @myId;
Run Code Online (Sandbox Code Playgroud)

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

show variables like '%slow%';
Run Code Online (Sandbox Code Playgroud)

或者可能:

show global variables like '%slow%';
Run Code Online (Sandbox Code Playgroud)