Home  >  Article  >  Database  >  Can we use "IF NOT IN" in MySQL procedures?

Can we use "IF NOT IN" in MySQL procedures?

王林
王林forward
2023-08-31 21:05:05876browse

我们可以在 MySQL 过程中使用“IF NOT IN”吗?

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);

Output

+-------------+
| 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);

Output

 +-----------------+
 | 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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:MySQL client optionsNext article:MySQL client options