- 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
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)
- Related Articles
- IF() function in a MySQL Select statement?
- MySQL case statement inside a select statement?
- Is there such thing as a SELECT with an IF/ELSE statement in MySQL?
- How can I use MySQL IF() function within SELECT statement?
- MySQL If statement with multiple conditions?
- Which is faster, a MySQL CASE statement or a PHP if statement?
- How MySQL SUM() function evaluates if it is used with SELECT statement that returns no matching rows?
- Display records with conditions set using if statement in UPDATE statement with MySQL
- MySQL Select Statement DISTINCT for Multiple Columns?
- How to use prepared statement for select query in Java with MySQL?
- How can I use a SELECT statement as an argument of MySQL IF() function?
- How to use NULL in MySQL SELECT statement?
- Get table names using SELECT statement in MySQL?
- MySQL procedure to display a “select” statement twice
- Delete rows with duplicate and similar content & get row with maximum number with MySQL select statement?

Advertisements