gpt4 book ai didi

MYSQL:If/Else 语法

转载 作者:行者123 更新时间:2023-11-29 06:27:36 25 4
gpt4 key购买 nike

正在处理查询,但我需要实现一些 if else 逻辑来检查循环,但我不断收到 1064 错误语法错误。

我已经阅读了手册和一些示例,但仍然没有运气。因此,我将查询简化为以下最基本的比较:

SET @start_date = '2019-06-01';
SET @other_date = CURDATE();

IF @start_date < @other_date THEN
Select 'True';
ELSE
Select 'False';
END IF;

以下是我从状态中收到的读数:

SET @start_date = '2019-06-01' OK Time: 0.065s

设置@other_date = CURDATE()

OK Time: 0.065s

IF @start_date < @other_date THEN选择“真”

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 @start_date < @other_date THEN Select 'True'' at line 1 Time: 0.067s

我知道我的 IF 子句语法在某个地方出错了,但在文档中它指出:

IF [条件] THENif 语句别的 else 语句;结束如果;

我不确定为什么会失败,任何见解将不胜感激。

最佳答案

对于查询,没有 IF...ELSE... 语句(您可以在存储过程/函数中使用)。
相反,您可以使用函数 IF() :

SELECT IF(@start_date < @other_date, 'True', 'False');

CASE表达式:

SELECT CASE WHEN @start_date < @other_date THEN 'True' ELSE 'False' END;

请参阅demo .

关于MYSQL:If/Else 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58598281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com