
- 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
How to implement ternary conditional operator in MySQL?
A ternary conditional operator looks like ?: in programming language like C, C++, Java etc. The syntax is as follows −
(yourCondition) ? statement1:statement2;
In the above syntax, if yourCondition becomes true then statement1 will evaluate and if yourCondition becomes false then statement2 will evaluate.
But the above syntax does not work in MySQL. We can use IF() function from MySQL for the same purpose.
Let us see an example −
Case 1
mysql> select if(3 > 5,'Condition is true','Condition is not true') as ConditionalResult;
The following is the output in which second statement evaluates since is 3 isn’t more than 5 −
+-----------------------+ | ConditionalResult | +-----------------------+ | Condition is not true | +-----------------------+ 1 row in set (0.00 sec)
Case 2
mysql> select if(3 < 5,'Condition is true','Condition is not true') as ConditionalResult;
The following is the output in which the first statement evaluates since 3 is less than 5 −
+-------------------+ | ConditionalResult | +-------------------+ | Condition is true | +-------------------+ 1 row in set (0.00 sec)
- Related Articles
- Conditional ternary operator ( ?: ) in C++
- What is a Ternary operator/conditional operator in C#?
- Does Python have a ternary conditional operator?
- How to overload python ternary operator?
- How Ternary operator in PowerShell Works?
- Implement Conditional MySQL Query in a stored procedure?
- Ternary Operator in Java
- Ternary Operator in C#
- Ternary Operator in Python?
- How to use the ?: conditional operator in C#?
- Changing ternary operator into non-ternary - JavaScript?
- Ternary Operator in Dart Programming
- Java Ternary Operator Examples
- C/C++ Ternary Operator
- Java Ternary Operator Puzzle

Advertisements