- 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
Write MySQL case statement to set custom messages for student’s result
For this, set conditions using MySQL CASE statement −
mysql> create table DemoTable1916 ( StudentName varchar(20), StudentMarks int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1916 values('Chris',59); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('David',89); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('Sam',94); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('Mike',75); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1916 values('Carol',69); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1916;
This will produce the following output −
+-------------+--------------+ | StudentName | StudentMarks | +-------------+--------------+ | Chris | 59 | | David | 89 | | Sam | 94 | | Mike | 75 | | Carol | 69 | +-------------+--------------+ 5 rows in set (0.00 sec)
Here is the query to set custom message for Student Marks −
mysql> select StudentName, case when StudentMarks > 70 Then 'Good Marks' else 'Not Good Marks' end as Result from DemoTable1916;
This will produce the following output −
+-------------+----------------+ | StudentName | Result | +-------------+----------------+ | Chris | Not Good Marks | | David | Good Marks | | Sam | Good Marks | | Mike | Good Marks | | Carol | Not Good Marks| +-------------+----------------+ 5 rows in set (0.00 sec)
- Related Articles
- Set custom messages for enum values in MySQL
- Using CASE statement in MySQL to display custom name for empty value
- MySQL CASE statement to place custom values in place of NULL
- MySQL IF() to display custom YES or NO messages
- Set custom messages on the basis of a column with student marks in MySQL
- How to write custom Python Exceptions with Error Codes and Error Messages?
- Set custom messages by working with MySQL IF Statements and SELECT in a user-defined variable
- Set multiple values for custom columns in MySQL?
- MySQL case statement inside a select statement?
- How to add custom messages to TestNG failure?
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- Write a MySQL query to check if field exists and then return the result set?
- Set a custom value for NULL or empty values in MySQL
- Why should we use MySQL CASE Statement?
- MySQL order by field using CASE Statement

Advertisements