如何在 MySQL 存储过程中实现 If else?
要实现 if-else,语法如下 −
if yourCondition then yourStatement1; else yourStatement2; end if ;
为了理解存储过程中 if-else 的上述概念,我们创建一个存储过程 −
mysql> delimiter // mysql> create procedure If_else_stored_demo(value int) begin if value > 1000 then select "your value is greater than 1000"; else select "your value is less than or equal to 1000"; end if ; end // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ;
现在,你可以使用 call 命令调用存储过程 −
mysql> call If_else_stored_demo(500);
这会产生以下输出 −
+------------------------------------------+ | your value is less than or equal to 1000 | +------------------------------------------+ | your value is less than or equal to 1000 | +------------------------------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec)
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP