作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我安装的 phpmyadmin 中,SQL 选项卡不处理 IF THEN 语句。但是,它确实可以正确处理 IF() 函数。例如,这很好用:
SET @thisyear = IF(YEAR(CURRENT_DATE) = 2018, 2018, 2019);
SELECT @thisyear AS ThisYear, `foobar`.`id` AS ID ...
但是,以下会产生错误:
IF year(CURRENT_DATE) = '2018' THEN
SET @thisyear = '2018';
END IF;
SELECT @thisyear AS ThisYear, `foobar`.`id` AS ID ...
这是错误信息:
#1064 - 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 year(CURRENT_DATE) = '2018' THEN
SET @thisyear = '2018'' at line 1
我的 phpmyadmin 配置是否有问题,或者我是否需要在使用 IF 语句之前先调用一些东西?提前致谢。
最佳答案
IF
是一种控制流语句,只允许在编程 block 中使用——存储过程、函数、触发器、事件。
IF()
是 SQL 语言的 MySQL 扩展,它是一个标量函数,可以在任何使用标量函数的地方使用。也就是说,您可以只使用标准的 CASE
表达式。
这种区别实际上在 documentation 中明确指出:
The IF statement for stored programs implements a basic conditional construct.
Note
There is also an IF() function, which differs from the IF statement described here. See Section 12.4, “Control Flow Functions”. The IF statement can have THEN, ELSE, and ELSEIF clauses, and it is terminated with END IF.
关于mysql - 在 phpmyadmin SQL 选项卡上使用 IF 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53934023/
我是一名优秀的程序员,十分优秀!