为此,您可以使用UNION ALL运算符。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Subject varchar(100) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Subject) values('MySQL'); mysql> insert into DemoTable(Subject) values('MySQL'); mysql> insert into DemoTable(Subject) values('MongoDB'); mysql> insert into DemoTable(Subject) values('MongoDB'); mysql> insert into DemoTable(Subject) values('MySQL'); mysql> insert into DemoTable(Subject) values('Java'); mysql> insert into DemoTable(Subject) values('MySQL'); mysql> insert into DemoTable(Subject) values('MongoDB');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+---------+ | 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)
以下是在MySQL中使用LIMIT实现多个WHERE的查询-
mysql> (select *from DemoTable where Subject='MySQL' limit 3) -> union all -> (select *from DemoTable where Subject='MongoDB' limit 5) -> ;
输出结果
+----+---------+ | Id | Subject | +----+---------+ | 1 | MySQL | | 2 | MySQL | | 5 | MySQL | | 3 | MongoDB | | 4 | MongoDB | | 8 | MongoDB | +----+---------+ 6 rows in set (0.00 sec)