If ELSE in MySQL
In this tutorial, we aim to explore how to use IF ELSE
the statement in MySQL.
One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering and visualization techniques.
One of them is IF ELSE
the statement. IF ELSE
The statement, as the name suggests, helps us filter the data of a specific table in the MySQL database.
These filtering conditions IF
are set in the block of statements. If the data entered in our table does not meet certain conditions, ELSE
the block is executed.
For example, in an employee table containing employee details, if we wish to filter employees based on their salary, we can use IF ELSE
the WITH clause in MySQL. Let us understand how this approach works.
Before we begin, let's student_details
create a dummy dataset by creating a table with a few rows.
-- create the table student_details
CREATE TABLE student_details(
stu_id int,
stu_firstName varchar(255) DEFAULT NULL,
stu_lastName varchar(255) DEFAULT NULL,
primary key(stu_id)
);
-- insert rows to the table student_details
INSERT INTO student_details(stu_id,stu_firstName,stu_lastName)
VALUES(1,"Preet","Sanghavi"),
(2,"Rich","John"),
(3,"Veron","Brow"),
(4,"Geo","Jos"),
(5,"Hash","Shah"),
(6,"Sachin","Parker"),
(7,"David","Miller");
To see the entries in the data, we use the following code.
SELECT * FROM student_details;
Output:
stu_id stu_firstName stu_lastName
1 Preet Sanghavi
2 Rich John
3 Veron Brow
4 Geo Jos
5 Hash Shah
6 Sachin Parker
7 David Miller
IF ELSE
Statements in MySQL
IF ELSE
The basic syntax of the technique is as follows.
select column_name,
(
CASE
WHEN <condition> THEN <operation>
ELSE 1
END)
from table_x;
As shown in the above query, we use case
the INSERT statement and ELSE
the SELECT clause. This is how the INSERT statement is executed in MySQL IF ELSE
.
Let's student_details
filter the data from the table, making sure that we print only the student's last name and not the first name when stu_id
is greater than . Otherwise, if is less than or equal to , we will print the first name.3
stu_id
3
We can achieve this using the following query.
select stu_id,
(
CASE
WHEN stu_id <= 3 THEN stu_firstName
ELSE stu_lastName
END) AS filtered_data
from student_details;
Output:
stu_id filtered_data
1 Preet
2 Rich
3 Veron
4 Jos
5 Shah
6 Parker
7 Miller
CASE ELSE
An alternative to the technique is stored procedures. It is possible to IF ELSE
create stored procedures using blocks, but this approach is very inefficient and case
is best suited for use cases similar to those discussed above.
Therefore, case
with the help of statement, we can effectively implement IF ELSE
the functionality expected from statement from any other programming language to filter data in MySQL.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
DATETIME vs. TIMESTAMP in MySQL
Publish Date:2025/04/11 Views:117 Category:MySQL
-
DATETIME and TIMESTAMP are two different data types that can be used to store values that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea
Execute multiple joins in one query in MYSQL
Publish Date:2025/04/11 Views:94 Category:MySQL
-
Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.
Joining 3 tables in MySQL
Publish Date:2025/04/11 Views:187 Category:MySQL
-
In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS
Use of UPDATE JOIN in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla
How to use the Row_Number() function in MySQL
Publish Date:2025/04/11 Views:142 Category:MySQL
-
In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti
Multiple primary keys in MySQL
Publish Date:2025/04/11 Views:66 Category:MySQL
-
In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b
Displaying foreign keys in MySQL
Publish Date:2025/04/11 Views:55 Category:MySQL
-
In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei
Select first N rows in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl
Copying a Database in MySQL
Publish Date:2025/04/11 Views:105 Category:MySQL
-
Creating a copy of an existing database is known as the MySQL Clone method. Cloning involves creating a copy of the table structure, constraints, functions, procedures, triggers, and all functionality associated with the table in one go. Th