您可以在 AND 或 OR 运算符的帮助下在具有多个条件的存储过程中使用 if 语句。语法如下 -
DECLARE X int;
DECLARE Y int;
SET X = value1;
SET Y = value2;
IF ( (X < Y AND X > value1 AND Y >value2) OR X! = anyValueToCompare) THEN
yourStatement;
ELSE
yourStatement;
END IF
现在为了理解上面的语法,让我们创建一个存储过程。创建存储过程的查询如下 -
mysql> create procedure SP_IFELSEDEMO()
-> BEGIN
-> DECLARE X int;
-> DECLARE Y int;
-> SET X=100;
-> SET Y=400;
-> IF ( (X < Y AND X > 99 AND Y >300) OR X! = 10 ) THEN
-> SELECT 'Logic is Correct';
-> ELSE
-> SELECT 'Logic is not Correct';
-> END IF;
-> END;
-> //
Query OK, 0 rows affected (0.27 sec)
mysql> DELIMITER ;
现在借助 CALL 命令调用存储过程。查询如下 -
mysql> call SP_IF ELSEDEMO();
输出
+------------------+
| Logic is Correct |
+------------------+
| Logic is Correct |
+------------------+
1 row in set (0.04 sec)
Query OK, 0 rows affected (0.07 sec)