在存储过程或函数之外使用 if 语句

5 mysql

我想调用一个查询来更新一个 mysql 表,但我的查询包含一个 if 语句

set @id = 0;
set @the_number = 0;
set @the_message = 0;
set @selected_message = 0;

SELECT id, dest_msisdn, text_message INTO @id, @the_number, @the_message FROM    incoming_sms where service_id = 6015592000101762 AND job_status = 0 limit 1;

if(@the_message LIKE '%Bank%')then

select 'h';

/* Update Statement Here*/
else 

select 'nothing to see here';

end if;
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误。

#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(@the_message LIKE '%Bank%')then

select 'h'' at line 1 
Run Code Online (Sandbox Code Playgroud)

在此处为if http://dev.mysql.com/doc/refman/5.0/en/if.html找到的 mysql 手册中, 它解释了The IF statement for stored programs implements a basic conditional construct....

If语句是否可以在存储过程之外使用?

Rol*_*DBA 3

使用IF() 函数

mysql> set @the_message = 'Rolando Bank of America';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT IF(@the_message LIKE '%Bank%','h','nothing here to see') result;
+--------+
| result |
+--------+
| h      |
+--------+
1 row in set (0.00 sec)

mysql> set @the_message = 'Rolando Bk of America';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT IF(@the_message LIKE '%Bank%','h','nothing here to see') result;
+---------------------+
| result              |
+---------------------+
| nothing here to see |
+---------------------+
1 row in set (0.00 sec)

mysql>
Run Code Online (Sandbox Code Playgroud)

试一试 !!!