
- 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 WHEN with SELECT to display odd and even ids?
Let us first create a table −
mysql> create table DemoTable ( PageId int ); Query OK, 0 rows affected (0.85 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(233); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values(34); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(76); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | PageId | +--------+ | 233 | | 34 | | 76 | | 89 | +--------+ 4 rows in set (0.00 sec)
Following is the query for CASE WHEN with SELECT −
mysql> select PageId, case when PageId %2 !=0 then 'PageId is odd' else 'PageId is even' end AS Result from DemoTable;
This will produce the following output −
+--------+----------------+ | PageId | Result | +--------+----------------+ | 233 | PageId is odd | | 34 | PageId is even| | 76 | PageId is even| | 89 | PageId is odd| +--------+----------------+ 4 rows in set (0.00 sec)
- Related Articles
- Display IDs in a particular order with MySQL IN()?
- Lower case column names with MySQL SELECT?
- Count duplicate ids and display the result in a separate column with MySQL
- MySQL ORDER BY with CASE WHEN
- Implement MySQL CASE statement with WHEN clause
- Alternative to MySQL CASE WHEN in MySQL
- Display highest amount from corresponding duplicate ids in MySQL
- MySQL Mass Update with CASE WHEN/ THEN/ ELSE?
- Perform count with CASE WHEN statement in MySQL?
- Select multiple sums with MySQL query and display them in separate columns?
- Select count of values (Yes, No) with same ids but different corresponding records in MySQL?
- Even numbers at even index and odd numbers at odd index in C++
- Store a variable with the result of a MySQL SELECT CASE?
- MySQL select for exact case sensitive match with hyphen in records
- Count subarrays with same even and odd elements in C++

Advertisements