Home > Database > Mysql Tutorial > body text

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

王林
Release: 2023-08-31 21:05:05
forward
856 people have browsed it

我们可以在 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    
Copy after login

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 ;
Copy after login

Now call the stored procedure using CALL command.

Case 1 - When value is found -

mysql> call IF_NOT_INDemo(10);
Copy after login

Output

+-------------+
| Value Found |
+-------------+
| Value Found |
+-------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)
Copy after login

Case 2 - When value is not found -

mysql> call IF_NOT_INDemo(100);
Copy after login

Output

 +-----------------+
 | Value Not Found |
 +-----------------+
 | Value Not Found |
 +-----------------+
1 row in set (0.05 sec)

Query OK, 0 rows affected (0.07 sec)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!