MySQL: IF-THEN-ELSEIF-ELSE Statement

Profile picture for user arilio666

The IF-THEN-ELSEIF-ELSE Statement in MySQL does pretty much what a conditional statement does in any other language here we will be using the stored procedure to give in the task.
As we all know that a stored procedure is a pre-compiled stored language ready for execution with its stored table within.

Now we won't be discussing much in-depth about the PROCEDURE rather more about the IF-THEN-ELSEIF-ELSE.

Now the IF does the operation of what we are specifying based on the given condition and now if the condition inside the IF block is not satisfied then come to ELSEIF which means when the condition inside the if block does not satisfy then go to the second one which is alternate to IF called ELSE IF secondary and all that fails to go to ELSE where there is no way.

It simply does this else if do that or else simply do this.

Syntax

IF Condition THEN
Query Statement;
ELSEIF THEN
Query Statement2;
ELSE
Query Statement3
END IF;

Let us see the real-time example of this IF-THEN-ELSEIF-ELSE for this we will be using the same crypto_price table.

So this is the table that will be used in the stored procedure and conditioned with the IF-THEN-ELSEIF-ELSE statement.

Let us dive into the Procedure functions we are about to create.

CREATE  PROCEDURE `cryptolist`(Coin_Name varchar(50))
BEGIN
IF Coin_Name='Doge'THEN
UPDATE crypto_price
SET Price=100
WHERE crypto_price.Name='Doge';
ELSEIF Coin_Name='XRP' THEN
UPDATE crypto_price
SET Price=98888
WHERE crypto_price.Name='XRP';
ELSE
Update crypto_price
SET Price=0
WHERE crypto_price.Name='XRP';
END IF;
select * from crypto_price;
END
  • So we have created a procedure named cryptolist and passed in the argument with the name Coin_Name.
  • So as we all know procedure always starts with BEGIN.
  • We have created the table as we all know how to query table creation and insert values into the respective data types.
  • We have created the table crypto_price.
  • Now we have used the IF-THEN-ELSE here.

Let see the explanation for it more briefly:

  • It is asked to do that IF the value passed in the argument when calling the procedure is 'Doge'.
  • Update the table we just created crypto_price to set the price to 100 for the respective table name. column name that is precise 'Doge'.
  • If the passed argument is not 'DOGE' then automatically it goes to the ELSEIF block and executes that query provided inside.
  • And even when the ELSEIF condition fails to satisfy it goes into the final ELSE block by default and satisfy that condition no matter it is true or false.
  • using the following query we can call the procedure and use the schema we created and stored to update the price of the crypto every time.

ELSE Scenario

call cryptolist('randomblabla');

The condition comes to pass this body when both the ELSEIF and IF are not the satisfied and set price of XRP to 0.

IF Scenario

call cryptolist('Doge')

Here we can see doge value is updated as the argument passed satisfies the IF block.

ELSEIF Scenario

call crypto_price('XRP')

When the argument passed inside is XRP then the ELSEIF gets executed.

Tags