【问题标题】:How to display the value of a variable at the commandline in MySQL?如何在 MySQL 的命令行中显示变量的值?
【发布时间】:2013-09-21 05:28:44
【问题描述】:

我尝试了以下 -

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

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

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

    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

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

【问题讨论】:

    标签: mysql variables command-line


    【解决方案1】:

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

    SELECT @myId;
    

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

    show variables like '%slow%';
    

    或者可能:

    show global variables like '%slow%';
    

    【讨论】:

      【解决方案2】:

      显示全局状态,例如“%com_stmt%”; 可用于使用通配符确定任何 SHOW GLOBAL STATUS 当前值。

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

      有超过 300 个 GLOBAL STATUS 值。

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

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

      【讨论】:

        【解决方案3】:

        只需SELECT这样的变量:

        SELECT @myId;
        

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

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

        【讨论】:

        • 好吧,我尝试在显示变量上进行谷歌搜索,而 PRINT 是我发现的唯一内容。
        • 好的,但是知道要搜索什么是你 26k 和我 2k 的原因。
        • 我认为知道要搜索的内容是让我达到 185 岁的原因。
        • 对于全局变量,这不起作用,请查看下面 Ryan Shillington 的解决方案。
        猜你喜欢
        • 1970-01-01
        • 2019-04-17
        • 1970-01-01
        • 2010-12-02
        • 1970-01-01
        • 1970-01-01
        • 2020-10-27
        相关资源
        最近更新 更多