mysql - 在 MySql 存储过程中使用 IF 和 Concat 时卡住

标签 mysql stored-procedures

我正在尝试将 IFMySql 存储过程 中的其他代码一起使用,但事情似乎并不顺利,我无法保存存储过程。如果我删除 IF 一切正常并且我能够保存存储过程。

问题出在 IF 那里。你能帮忙吗?

错误

/* SQL Error (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 FIND_IN_SET (10,vals) THEN
                'Grey Link'
            ELSE
                SET ret_val = 'Blue' at line 20 */

存储过程

BEGIN

DECLARE vals VARCHAR(2400);
DECLARE ret_val VARCHAR(2400);

select group_concat(elmAction) into vals from reports where id = this_id and userId = this_user;

SET ret_val = CASE this_id
    WHEN 1 THEN CONCAT (
        'Blue Type 1'
        'Blue Type 2'
        'Blue Type 3' #Here is where the problem is...
            IF FIND_IN_SET (10,vals) THEN
                'Grey Link'
            ELSE
                'Blue link'
            END IF;
        )
ELSE 'Error'
END;


RETURN (ret_val);

END

最佳答案

试试这个:

CONCAT (
        'Blue Type 1',
        'Blue Type 2',
        'Blue Type 3',
        IF( FIND_IN_SET (10,vals), 'Grey Link','Blue link')
)

使用 CASE :

CONCAT (
        'Blue Type 1',
        'Blue Type 2',
        'Blue Type 3',
        CASE
            WHEN FIND_IN_SET (10,vals) = 0 THEN 'Blue link'
            WHEN FIND_IN_SET (10,vals) = 1 THEN ....
            WHEN FIND_IN_SET (10,vals) = 2 THEN ....
        ELSE
            ...
        END
)

关于mysql - 在 MySql 存储过程中使用 IF 和 Concat 时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18846655/

相关文章:

cron - MySQL 数据库备份 - 安全的方式

mysql - 在另一个 where 语句(子查询?)中使用一个 sql 查询的结果

sql - Firebird 将输入参数传递给 gen_id 函数

MySQL 服务器拒绝通过 SSH 隧道进行本地连接

mysql - Rails 和参照完整性

sql - 如何编写干净的 SQL Server 存储过程

stored-procedures - 在哪里更改 oracle 11g 中的 NLS_DATE_FORMAT?

sql-server - 在更新语句中引发错误

mysql - 计算集合的密度

mysql - 如何编写递归 MySQL 查询来连续跟踪项目管理任务记录