Skip to main content
Home
  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact
programsbuzz facebook programsbuzz twitter programsbuzz linkedin
  • Log in

Main navigation

  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact

MySQL: IF-THEN Statement

Profile picture for user arilio666
Written by arilio666 on 11/26/2021 - 12:43

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.

Syntax

IF Condition THEN
Query Statement;
END IF;

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 DEFINER=`root`@`localhost` PROCEDURE `cryptolist`(Coin_Name varchar(50))
BEGIN
create table crypto_price_list(
ID int,
Name varchar(50),
Price decimal(15,3)
);
insert into crypto_price_list(ID, Name, Price)
values(1,'Doge',23.22);
insert into crypto_price_list(ID, Name, Price)
values(2,'XRP',9.3);
insert into crypto_price_list(ID, Name, Price)
values(3,'SafeMoon',77.3);

IF Coin_Name='XRP'THEN
UPDATE crypto_price_list
SET Price=2100.00
WHERE crypto_price_list.Name='XRP';
END IF;
select * from crypto_price_list;
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_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');

Related Content
MySQL Tutorial
MySQL: Exporting Data using SELECT ... INTO OUTFILE
MySQL: Import CSV File in Database or Table
Tags
MySQL
  • Log in or register to post comments

Choose Your Technology

  1. Agile
  2. Apache Groovy
  3. Apache Hadoop
  4. Apache HBase
  5. Apache Spark
  6. Appium
  7. AutoIt
  8. AWS
  9. Behat
  10. Cucumber Java
  11. Cypress
  12. DBMS
  13. Drupal
  14. GitHub
  15. GitLab
  16. GoLang
  17. Gradle
  18. HTML
  19. ISTQB Foundation
  20. Java
  21. JavaScript
  22. JMeter
  23. JUnit
  24. Karate
  25. Kotlin
  26. LoadRunner
  27. matplotlib
  28. MongoDB
  29. MS SQL Server
  30. MySQL
  31. Nightwatch JS
  32. PactumJS
  33. PHP
  34. Playwright
  35. Playwright Java
  36. Playwright Python
  37. Postman
  38. Project Management
  39. Protractor
  40. PyDev
  41. Python
  42. Python NumPy
  43. Python Pandas
  44. Python Seaborn
  45. R Language
  46. REST Assured
  47. Ruby
  48. Selenide
© Copyright By iVagus Services Pvt. Ltd. 2023. All Rights Reserved.

Footer

  • Cookie Policy
  • Privacy Policy
  • Terms of Use