
- 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
MySQL query to return a string as a result of IF statement?
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, EmployeeSalary int ); Query OK, 0 rows affected (1.68 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(EmployeeSalary) values(12000); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(EmployeeSalary) values(20000); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable(EmployeeSalary) values(11500); Query OK, 1 row affected (0.94 sec) mysql> insert into DemoTable(EmployeeSalary) values(15500); Query OK, 1 row affected (0.44 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+----------------+ | Id | EmployeeSalary | +----+----------------+ | 1 | 12000 | | 2 | 20000 | | 3 | 11500 | | 4 | 15500 | +----+----------------+ 4 rows in set (0.00 sec)
Following is the query to return a string as a result of IF statement −
mysql> select Id,if(EmployeeSalary <= 12000 ,'Internship Employee','FullTime Employee') as status from DemoTable;
This will produce the following output −
+----+---------------------+ | Id | status | +----+---------------------+ | 1 | Internship Employee | | 2 | FullTime Employee | | 3 | Internship Employee | | 4 | FullTime Employee | +----+---------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Write a MySQL query to check if field exists and then return the result set?
- How to get file extension of file as a result of MySQL query?
- MySQL query to check if a string contains a word?
- Set the result of a query to a variable in MySQL?
- Can we return query results in same order as the values in MySQL `IN(…)` statement?
- Returning a value even if there is no result in a MySQL query?
- How to assign the result of a MySQL query into a variable?
- Program to multiply two strings and return result as string in C++
- MySQL query to return a substring after delimiter?
- How to store Query Result in a variable using MySQL?
- What is the proper way to insert an IF statement into a MySQL query?
- IF ELSE statement in a MySQL Statement?
- Setting column values as column names in the MySQL query result?
- MySQL query to search a string ‘demo’ if someone mistakenly types ‘deom’?
- MySQL query to return the entire date and time based on a string and format

Advertisements