
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Use IF Clause in MySQL to Display Students' Result as Pass or Fail
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100), -> Subject varchar(100), -> Score int -> ); Query OK, 0 rows affected (0.94 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name,Subject,Score) values('Chris','MySQL',80); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable(Name,Subject,Score) values('Robert','MongoDB',45); Query OK, 1 row affected (0.62 sec) mysql> insert into DemoTable(Name,Subject,Score) values('Adam','Java',78); Query OK, 1 row affected (0.52 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+----+--------+---------+-------+ | Id | Name | Subject | Score | +----+--------+---------+-------+ | 1 | Chris | MySQL | 80 | | 2 | Robert | MongoDB | 45 | | 3 | Adam | Java | 78 | +----+--------+---------+-------+ 3 rows in set (0.00 sec)
Here is the query to work with if clause in MySQL to display result in the form of Pass or Fail.
mysql> select Name,Subject,Score,if(Score > 75,"PASS","FAIL") AS Status from DemoTable;
Output
This will produce the following output −
+--------+---------+-------+--------+ | Name | Subject | Score | Status | +--------+---------+-------+--------+ | Chris | MySQL | 80 | PASS | | Robert | MongoDB | 45 | FAIL | | Adam | Java | 78 | PASS | +--------+---------+-------+--------+ 3 rows in set (0.00 sec)
Advertisements