
- 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
Multiple WHERE with LIMIT in MySQL?
For this, you can use UNION ALL operator. Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Subject varchar(100) -> ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable(Subject) values('Java'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+---------+ | Id | Subject | +----+---------+ | 1 | MySQL | | 2 | MySQL | | 3 | MongoDB | | 4 | MongoDB | | 5 | MySQL | | 6 | Java | | 7 | MySQL | | 8 | MongoDB | +----+---------+ 8 rows in set (0.00 sec)
Following is the query to implement multiple WHERE with LIMIT in MySQL −
mysql> (select *from DemoTable where Subject='MySQL' limit 3) -> union all -> (select *from DemoTable where Subject='MongoDB' limit 5) -> ;
Output
+----+---------+ | Id | Subject | +----+---------+ | 1 | MySQL | | 2 | MySQL | | 5 | MySQL | | 3 | MongoDB | | 4 | MongoDB | | 8 | MongoDB | +----+---------+ 6 rows in set (0.00 sec)
- Related Articles
- MySQL select query with multiple WHERE?
- Update with multiple values in MySQL WHERE clause
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?
- Can we fetch multiple values with MySQL WHERE Clause?
- Working with MySQL WHERE.. OR query with multiple OR usage. Is there an alternative?
- Get another order after limit with MySQL?
- MySQL multiple COUNT with multiple columns?
- How to update multiple rows using single WHERE clause in MySQL?
- Which rows are returned while using LIMIT with OFFSET in MySQL?
- Is it possible to use UPDATE query with LIMIT in MySQL?
- How to limit maximum items on multiple input ()?
- MySQL If statement with multiple conditions?
- Multiple LIKE Operators with ORDER BY in MySQL?
- Pagination using MySQL LIMIT, OFFSET?
- Multiple Where clause in C# Linq

Advertisements