Home > Database > Mysql Tutorial > body text

Does MySQL If statement have multiple conditions?

WBOY
Release: 2023-09-03 22:29:02
forward
1127 people have browsed it

MySQL If 语句有多个条件吗?

You can use if statement in a stored procedure with multiple conditions with the help of AND or OR operator. The syntax is as follows -

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

Now to understand the above syntax, let us create a stored procedure. The query to create the stored procedure is as follows -

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 &#39;Logic is Correct&#39;;
   -> ELSE
   ->    SELECT &#39;Logic is not Correct&#39;;
   -> END IF;
   -> END;
   -> //
Query OK, 0 rows affected (0.27 sec)
mysql> DELIMITER ;
Copy after login

Now call the stored procedure with the help of CALL command. The query is as follows -

mysql> call SP_IF ELSEDEMO();
Copy after login

Output

+------------------+
| Logic is Correct |
+------------------+
| Logic is Correct |
+------------------+
1 row in set (0.04 sec)
Query OK, 0 rows affected (0.07 sec)
Copy after login

The above is the detailed content of Does MySQL If statement have multiple conditions?. 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