

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- MySQL ORDER BY with CASE WHEN
- Implement MySQL CASE statement with WHEN clause
- Lower case column names with MySQL SELECT?
- Display IDs in a particular order with MySQL IN()?
- Alternative to MySQL CASE WHEN in MySQL
- MySQL Mass Update with CASE WHEN/ THEN/ ELSE?
- Perform count with CASE WHEN statement in MySQL?
- Count duplicate ids and display the result in a separate column with MySQL
- Even numbers at even index and odd numbers at odd index in C++
- Separate odd and even in JavaScript
- Signals and Systems: Even and Odd Signals
- Count subarrays with same even and odd elements in C++
- Sorting odd and even elements separately JavaScript
- Even and Odd Components of a Signal
- Display highest amount from corresponding duplicate ids in MySQL
Advertisements