
- 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 case statement inside a select statement?
For this, you can use CASE WHEN statement. Let us first create a table −
mysql> create table DemoTable -> ( -> FirstName varchar(20), -> Score int -> ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John',46); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('John',78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('John',69); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris',78); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Chris',89); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------+ | FirstName | Score | +-----------+-------+ | John | 46 | | John | 78 | | John | 69 | | Chris | 78 | | Chris | 89 | +-----------+-------+ 5 rows in set (0.00 sec)
Following is the query to implement case statement inside a select statement −
mysql> select FirstName,max(case when Score=69 then 1 else 0 end ) as isExistsOrNot -> from DemoTable -> group by FirstName;
This will produce the following output −
+-----------+---------------+ | FirstName | isExistsOrNot | +-----------+---------------+ | John | 1 | | Chris | 0 | +-----------+---------------+ 2 rows in set (0.00 sec)
- Related Articles
- Can we use WHERE clause inside MySQL CASE statement?
- Which is faster, a MySQL CASE statement or a PHP if statement?
- Using the value of an alias inside the same MySQL SELECT statement
- Change value from 1 to Y in MySQL Select Statement using CASE?
- IF() function in a MySQL Select statement?
- MySQL SELECT IF statement with OR?
- Why should we use MySQL CASE Statement?
- MySQL order by field using CASE Statement
- Implement MySQL CASE statement with WHEN clause
- MySQL procedure to display a “select” statement twice
- MySQL Select Statement DISTINCT for Multiple Columns?
- In which conditions, MySQL CASE statement return NULL?
- Perform count with CASE WHEN statement in MySQL?
- Storing value from a MySQL SELECT statement to a variable?
- How to use NULL in MySQL SELECT statement?

Advertisements