[SOLVED] MySQL If statement with multiple conditions

Issue

I want to write IF statement inside a Stored procedure in MySQL in the following way:

IF (exp1 and exp2 and exp3) or exp4

I know MySQL will treat IF() as a function call.
But I hope you got what I’m trying to achieve.
I am new to MySQL syntax.

Solution

In a procedure the use of an IF is pretty straight forward:

IF (yourCondition [logical operator(OR, AND) another condition] ) THEN

So in a practical example:

....
DECLARE m integer;
DECLARE n integer;
SET m = 1;
SET n = 0;
IF ((m>n AND m=1 AND n=0) OR m=n)THEN
     some code here
END IF;

The evaluation of the conditions follows the parenthesis rule same as in a mathematical operation.

You can refer to the Docs

Answered By – Jorge Campos

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published.