【问题标题】:IF syntax errorIF 语法错误
【发布时间】:2016-05-23 19:58:42
【问题描述】:

我在遵循IF 语法的 MySQL 指南时遇到语法错误。

我的查询是:

if 0=0 then select 'hello world'; end if;

从逻辑上讲,这应该选择'hello world',但我得到了

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 'if (0=0) then select 'hello world'' at line 1
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 'end if' at line 1

【问题讨论】:

    标签: mysql sql if-statement syntax syntax-error


    【解决方案1】:

    像这样使用 if 语句只在存储过程或函数内部有效。

    您可能想使用if() 函数,然后您可以使用:

    select IF(0=0, 'hello world','');
    

    【讨论】:

      【解决方案2】:

      一般 SQL 流不支持 If 语句。它只能在函数或过程中使用。但是您可以通过以下方式使用它

      use my_db;
      delimiter #
      create procedure xyz()
       begin
         IF 0=0 then select 'hello world';
       end if;
      end#
      

      【讨论】:

      • 为什么不直接使用内置的 IF() 函数?
      • 是的,有可能,但我已经根据问题的上下文回答了!
      【解决方案3】:

      您的查询仅在存储过程/函数上下文中有效。 参考there

      【讨论】:

        猜你喜欢
        • 2011-09-22
        • 2010-10-22
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        • 1970-01-01
        • 2012-05-02
        • 1970-01-01
        相关资源
        最近更新 更多