Friday , March 29 2024

MySQL IF Statement and MySQL IF() Function

The article contains information about MYSQL IF Statement, ELSEIF Statement and ELSE STATEMENT used to direct the command flow by condition in the MySQL VTY system.

MySQL IF() Function

We can use two if statements: MySQL if function and if statement.

MySQL if function is used in SQL commands. Similar to the ternary operator statement in programming languages.

If the condition written to the first parameter of the MySQL IF function is true, it runs the TRUE parameter, and if it is false, it runs the FALSE parameter.

Using MySQL IF function;

The IF function is also used in table queries.
The query will return “Expensive” as a result if the price in the product_price column is greater than 30, and “Inexpensive” if the product_price column is is smaller than 30.

SQL functions can also be used within the MySQL IF function.

The query will return LONG if the product name length in the product_name column is greater than 10 characters, and SHORT if product_name column is smaller than 10 characters.

MySQL IF-ELSEIF-ELSE Statement

This allows using if, elseif and else statements to check the values ​​in MySQL Stored Procedures.

MySQL If Usage;

MySQL ElseIf Usage;

If Condition in IF is wrong AND Condition2 in ELSEIF is true, commands in Command2 are executed.

MySQL Else Usage;

or
If all IF and ELSEIF conditions are wrong, commands3 in the ELSE section are executed.

Below is an example of using if, elseif and else in stored procedures.

In the example, the NameSurname stored procedure returns different status values according to the MyNumber parameter.

If the MyNumber parameter value is 1, the status parameter returns Yusuf, 2 if SEZER, 3 if Yusuf SEZER, if none, Yusuf Sefa SEZER.

Let’s run the stored procedure.

When the stored procedure is executed, it will return “Yusuf Sefa SEZER”.

Loading

About Yusuf SEZER

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories