 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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 SELECT IF statement with OR?
You can use SELECT IF statement with OR. To understand select with OR, let us create a table. The query to create a table is as follows −
mysql> create table EmployeeInformation -> ( -> EmployeeId int, -> EmployeeName varchar(100), -> EmployeeStatus varchar(100) -> ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into EmployeeInformation values(1,'Sam','FullTime'); Query OK, 1 row affected (0.23 sec) mysql> insert into EmployeeInformation values(2,'Mike','PartTime'); Query OK, 1 row affected (0.14 sec) mysql> insert into EmployeeInformation values(3,'Bob','Intern'); Query OK, 1 row affected (0.14 sec) mysql> insert into EmployeeInformation values(4,'Carol','FullTime'); Query OK, 1 row affected (0.16 sec) mysql> insert into EmployeeInformation values(5,'John','FullTime'); Query OK, 1 row affected (0.19 sec) mysql> insert into EmployeeInformation values(6,'Johnson','PartTime'); Query OK, 1 row affected (0.19 sec) mysql> insert into EmployeeInformation values(7,'Maria','Intern'); Query OK, 1 row affected (0.12 sec)
Let us now display all records from the table using select command. The query is as follows −
mysql> select *from EmployeeInformation;
Output
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeStatus | +------------+--------------+----------------+ | 1 | Sam | FullTime | | 2 | Mike | PartTime | | 3 | Bob | Intern | | 4 | Carol | FullTime | | 5 | John | FullTime | | 6 | Johnson | PartTime | | 7 | Maria | Intern | +------------+--------------+----------------+ 7 rows in set (0.00 sec)
Here is the query to perform SELECT IF statement with OR. In the below query, you will get only EmployeeName which has EmployeeStatus FullTime and Intern, else you will get status of the employee.
The query is as follows −
mysql> select if(EmployeeStatus='FullTime' or EmployeeStatus='Intern',EmployeeName,EmployeeStatus) as Status from EmployeeInformation;
Output
+----------+ | Status | +----------+ | Sam | | PartTime | | Bob | | Carol | | John | | PartTime | | Maria | +----------+ 7 rows in set (0.00 sec)
Advertisements
                    