- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
How to use “OR” condition in MySQL CASE expression?
Set the same condition like “OR” in a MySQL CASE expression. Let us first create a sample table.
Following is the query
mysql> create table caseOrConditionDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100), -> Score int -> ); Query OK, 0 rows affected (0.49 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into caseOrConditionDemo(Name,Score) values('Larry',85); Query OK, 1 row affected (0.18 sec) mysql> insert into caseOrConditionDemo(Name,Score) values('Sam',74); Query OK, 1 row affected (0.20 sec) mysql> insert into caseOrConditionDemo(Name,Score) values('Mike',76); Query OK, 1 row affected (0.16 sec) mysql> insert into caseOrConditionDemo(Name,Score) values('Carol',65); Query OK, 1 row affected (0.20 sec)
Following is the query to display records from the table using select command:
mysql> select *from caseOrConditionDemo;
This will produce the following output
+----+-------+-------+ | Id | Name | Score | +----+-------+-------+ | 1 | Larry | 85 | | 2 | Sam | 74 | | 3 | Mike | 76 | | 4 | Carol | 65 | +----+-------+-------+ 4 rows in set (0.00 sec)
Following is the query to use a condition like “OR” in MySQL CASE expression:
mysql> select Id,Name,Score, -> case when Score > 75 then 'Better Score' -> when Score > 70 then 'Good Score' -> else 'Not Good Score' -> end as 'Performance' -> from caseOrConditionDemo;
This will produce the following output
+----+-------+-------+----------------+ | Id | Name | Score | Performance | +----+-------+-------+----------------+ | 1 | Larry | 85 | Better Score | | 2 | Sam | 74 | Good Score | | 3 | Mike | 76 | Better Score | | 4 | Carol | 65 | Not Good Score | +----+-------+-------+----------------+ 4 rows in set (0.04 sec)
Advertisements