MySQL 中多个 WHERE 和 LIMIT?
mysqlmysqli database
为此,您可以使用 UNION ALL 运算符。让我们首先创建一个表 −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Subject varchar(100) -> ); Query OK, 0 rows affected (0.64 sec)
使用 insert 命令在表中插入一些记录 −
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)
使用 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)