IF语法错误

时间:2016-02-12 15:38:08

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

我在遵循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

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

使用if这样的语句仅在存储过程或函数内有效。

您可能想要使用的是if()功能,然后您可以使用:

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

答案 2 :(得分:0)

如果一般SQL流程不支持语句。它只能用于功能或程序。但是,您可以按照以下方式使用它

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