
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Implement MySQL query using multiple OR statements. Any optimal alternative?
It would be good to use IN() instead of multiple OR statements. Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstName varchar(20) -> ); Query OK, 0 rows affected (0.83 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable(FirstName) values('David'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable(FirstName) values('Bob'); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Adam | | 3 | David | | 4 | Mike | | 5 | Bob | +----+-----------+ 5 rows in set (0.00 sec)
Here is the query for using IN() −
mysql> select *from DemoTable where Id IN(2,3,5);
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 2 | Adam | | 3 | David | | 5 | Bob | +----+-----------+ 3 rows in set (0.00 sec)
- Related Articles
- Working with MySQL WHERE.. OR query with multiple OR usage. Is there an alternative?
- Implement multiple COUNT() in a single MySQL query
- MySQL query to get result from multiple select statements?
- Implement multiple LIKE operators in a single MySQL query
- Is there any alternative for CONCAT() in MySQL?
- Delete selective multiple records using MySQL DELETE query
- MySQL query to find alternative records from a table
- How can I avoid too many OR statements in a MySQL query?
- Implement case sensitivity in MySQL SELECT statements
- How to implement MongoDB $or query?
- MySQL query to return multiple row records with AND & OR operator
- Is there any easy way to add multiple records in a single MySQL query?
- Largest of two distinct numbers without using any conditional statements or operators
- MySQL query to update every alternative row string having same values?
- Implement MySQL ORDER BY without using ASC or DESC?

Advertisements