
- 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 can column data be used within MySQL CASE statement?
To understand it consider the data, as follows, from the table ‘Students’ −
mysql> Select * from Students; +----+-----------+-----------+----------+----------------+ | id | Name | Country | Language | Course | +----+-----------+-----------+----------+----------------+ | 1 | Francis | UK | English | Literature | | 2 | Rick | USA | English | History | | 3 | Correy | USA | English | Computers | | 4 | Shane | France | French | Computers | | 5 | Validimir | Russia | Russian | Computers | | 6 | Steve | Australia | English | Geoinformatics | | 7 | Rahul | India | Hindi | Yoga | | 8 | Harshit | India | Hindi | Computers | | 9 | Harry | NZ | English | Electronics | +----+-----------+-----------+----------+----------------+ 9 rows in set (0.00 sec)
Now, suppose if we want to know that how many of the students belong to USA, UK, NZ, INDIA, RUSSIA, FRANCE and so on then we can use column ‘country’ within CASE statement as follows −
mysql> Select SUM(CASE WHEN country = 'USA' THEN 1 ELSE 0 END) AS USA, -> SUM(CASE WHEN country = 'UK' THEN 1 ELSE 0 END) AS UK, -> SUM(CASE WHEN country = 'INDIA' THEN 1 ELSE 0 END) AS INDIA, -> SUM(CASE WHEN country = 'Russia' THEN 1 ELSE 0 END) AS Russia, -> SUM(CASE WHEN country = 'France' THEN 1 ELSE 0 END) AS France, -> SUM(CASE WHEN country = 'NZ' THEN 1 ELSE 0 END) AS NZ, -> SUM(CASE WHEN country = 'Australia' THEN 1 ELSE 0 END) AS Australia -> From Students; +------+------+-------+--------+--------+------+-----------+ | USA | UK | INDIA | Russia | France | NZ | Australia | +------+------+-------+--------+--------+------+-----------+ | 2 | 1 | 2 | 1 | 1 | 1 | 1 | +------+------+-------+--------+--------+------+-----------+ 1 row in set (0.07 sec)
- Related Articles
- How Can MySQL CASE statement be used in stored procedure?
- How MySQL IF statement can be used in a stored procedure?
- How MySQL WHILE loop statement can be used in stored procedure?
- How MySQL REPEAT loop statement can be used in stored procedure?
- How Can MySQL LOOP statement be used in a stored procedure?
- How MySQL IF ELSE statement can be used in a stored procedure?
- How can I use MySQL IF() function within SELECT statement?
- How can MySQL IF ELSEIF ELSE statement be used in a stored procedure?
- Can a number be used to name a MySQL table column?
- What is the meaning of “SELECT” statement in MySQL and how can it be used?
- Can we use WHERE clause inside MySQL CASE statement?
- Which MySQL Data Type can be used to store Negative Number?
- MySQL case statement inside a select statement?
- How can column data values of a table be compared using MySQL STRCMP() function?
- How can Tensorflow text be used to preprocess text data?

Advertisements