MySQL: IF-THEN Statement

Profile picture for user arilio666

The IF-THEN 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.
The IF-THEN acts like a statement where we will be telling the procedure to do the following thing in the table with provided conditions beside IF.
Now if the Condition is satisfied the body statement provided within it gets executed.


IF Condition THEN
Query Statement;

Now let us see them in action.
For this conditional statement series, we will be using a table that will be created now during the procedure called crypto_price_list.

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

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

CREATE  PROCEDURE `cryptolist`(Coin_Name varchar(50))
create table crypto_price_list(
ID int,
Name varchar(50),
Price decimal(15,3)
insert into crypto_price_list(ID, Name, Price)
insert into crypto_price_list(ID, Name, Price)
insert into crypto_price_list(ID, Name, Price)

IF Coin_Name='XRP'THEN
UPDATE crypto_price_list
SET Price=2100.00
WHERE crypto_price_list.Name='XRP';
select * from crypto_price_list;
  • 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_list.
  • Now we have used the IF-THEN 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 'XRP'.
  • Update the table we just created crypto_price_list to set the price to 2100.00 for the respective table name.column name that is precise 'XRP'.

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.

call cryptolist('XRP');