- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL If statement with multiple conditions?
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
Now to understand the above syntax, let us create a stored procedure. The query to create a 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 'Logic is Correct'; -> ELSE -> SELECT 'Logic is not Correct'; -> END IF; -> END; -> // Query OK, 0 rows affected (0.27 sec) mysql> DELIMITER ;
Now call the stored procedure with the help of the CALL command. The query is as follows −
mysql> call SP_IF ELSEDEMO();
Output
+------------------+ | Logic is Correct | +------------------+ | Logic is Correct | +------------------+ 1 row in set (0.04 sec) Query OK, 0 rows affected (0.07 sec)
- Related Articles
- Display records with conditions set using if statement in UPDATE statement with MySQL
- How to use multiple conditions in one if statement in Python?
- Update multiple values in a table with MySQL IF Statement
- How to override multiple if-else conditions using a switch statement in TypeScript?
- How to create conditions in a MySQL table with multiple columns?
- SHOW TABLE statement with multiple LIKE values in MySQL?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- MySQL SELECT IF statement with OR?
- In which conditions, MySQL CASE statement return NULL?
- How to work multiple AND conditions in MySQL?
- Insert multiple sets of values in a single statement with MySQL?
- MySQL Select Statement DISTINCT for Multiple Columns?
- MongoDB query to $pull / $unset with multiple conditions?
- Java switch statement with multiple cases.
- IF ELSE statement in a MySQL Statement?

Advertisements