- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Expression in CASE WHEN Clause doesn't work in MySQL query?
Fir this, use CASE WHEN statement in MySQL correctly. Let us see how.
Let us create a table −
mysql> create table demo58 −> ( −> id int not null auto_increment primary key, −> first_name varchar(20), −> last_name varchar(20) −> ); Query OK, 0 rows affected (2.15 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo58(first_name,last_name) values('John','Doe'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo58(first_name,last_name) values('David','Smith'); Query OK, 1 row affected (0.29 sec) mysql> insert into demo58(first_name,last_name) values('John','Brown'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo58(first_name,last_name) values('David','Miller'); Query OK, 1 row affected (0.26 sec)
Display records from the table using select statement −
mysql> select *from demo58;
This will produce the following output −
+----+------------+-----------+ | id | first_name | last_name | +----+------------+-----------+ | 1 | John | Doe | | 2 | David | Smith | | 3 | John | Brown | | 4 | David | Miller | +----+------------+-----------+ 4 rows in set (0.15 sec)
Following is the query for expression in CASE WHEN −
mysql> select −> ( −> case −> when (first_name = "John") then 'Carol' −> when (last_name ="Smith") then 'Doe' −> end −> ) RESULT −> from demo58;
This will produce the following output −
+--------+ | RESULT | +--------+ | Carol | | Doe | | Carol | | NULL | +--------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL system variable table_type doesn't work?
- Python3 - Why loop doesn't work?
- Adding a column that doesn't exist in a query?
- CSS content: attr() on HTML5 progress doesn't work
- HTML5 meta name = “viewport” doesn't work as expected
- Java Regular Expression that doesn't contain a certain String.
- New line separator doesn't work for group_concat function in MySQL? How to use it correctly?
- Why doesn't MySQL support millisecond / microsecond precision?
- MySQL query to include more than one column in a table that doesn't already exist
- Implement MySQL CASE statement with WHEN clause
- Java Regular Expression to match a line that doesn't contain a word.
- Why doesn't height: 100% work to expand divs to the screen height?
- Why doesn't JavaScript support multithreading?
- MySQL LIKE command doesn't work with strings containing dots to display records beginning with a specific number
- Why doesn't C++ support functions returning arrays

Advertisements