Let us first look at the syntax of IF NOT IN in MySQL -
if(yourVariableName NOT IN (yourValue1,yourValue2,........N) ) then statement1 else statement2 endif
Let us implement the above syntax to use IF NOT IN -
mysql> DELIMITER // mysql> CREATE PROCEDURE IF_NOT_INDemo(IN value int) -> BEGIN -> if(value NOT IN (10,20,30) ) then -> select "Value Not Found"; -> else -> select "Value Found"; -> end if; -> END -> // Query OK, 0 rows affected (0.25 sec) mysql> DELIMITER ;
Now call the stored procedure using CALL command.
Case 1 - When value is found -
mysql> call IF_NOT_INDemo(10);
+-------------+ | Value Found | +-------------+ | Value Found | +-------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec)
Case 2 - When value is not found -
mysql> call IF_NOT_INDemo(100);
+-----------------+ | Value Not Found | +-----------------+ | Value Not Found | +-----------------+ 1 row in set (0.05 sec) Query OK, 0 rows affected (0.07 sec)
The above is the detailed content of Can we use "IF NOT IN" in MySQL procedures?. For more information, please follow other related articles on the PHP Chinese website!