
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
IF ELSE statement in a MySQL Statement?
In an If-Else statement, the condition is evaluated to be true or false depending on the value.
Let us see an example. Firstly, we will create a table. The CREATE command is used to create a table.
mysql> create table IfelseDemo - > ( - > id int, - > FirstName varchar(100) - > ); Query OK, 0 rows affected (0.46 sec)
Records are inserted with the help of INSERT command.
mysql> insert into IfelseDemo values(1,'John'); Query OK, 1 row affected (0.13 sec) mysql> insert into IfelseDemo values(2,'Carol'); Query OK, 1 row affected (0.31 sec) mysql> insert into IfelseDemo values(3,'John'); Query OK, 1 row affected (0.11 sec) mysql> insert into IfelseDemo values(4,'Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into IfelseDemo values(5,'John'); Query OK, 1 row affected (0.11 sec)
Displaying all records.
mysql> select *from IfelseDemo;
Here is our output.
+------+-----------+ | id | FirstName | +------+-----------+ | 1 | John | | 2 | Carol | | 3 | John | | 4 | Carol | | 5 | John | +------+-----------+ 5 rows in set (0.00 sec)
The following is the query for using the if-else statement.
mysql> SELECT id, FirstName, (case when (id = 2 and FirstName = 'Carol') - > then - > 'Welcome Carol' - > else - > 'You are not Carol with id 2' - >end)as Message from IfelseDemo;
The following is the output.
+------+-----------+-----------------------------+ | id | FirstName | Message | +------+-----------+-----------------------------+ | 1 | John | You are not Carol with id 2 | | 2 | Carol | Welcome Carol | | 3 | John | You are not Carol with id 2 | | 4 | Carol | You are not Carol with id 2 | | 5 | john | You are not Carol with id 2 | +------+-----------+-----------------------------+ 5 rows in set (0.00 sec)
- Related Articles
- Java if-else statement
- Java if-then-else statement
- Java if-else-if ladder statement
- How MySQL IF ELSE statement can be used in a stored procedure?
- What is if...else if... statement in JavaScript?
- Explain if-else statement in C language
- How can MySQL IF ELSEIF ELSE statement be used in a stored procedure?
- What is the if...else statement in JavaScript?
- Explain Nested if-else statement in C language
- Explain else-if ladder statement in C language
- Is there such thing as a SELECT with an IF/ELSE statement in MySQL?
- How to indent an if...else statement in Python?
- How to show if...else statement using a flowchart in JavaScript?
- How to write an if-else statement in a JSP page?
- What is the ‘if...else if...else’ statement in Java and how to use it?

Advertisements