이를 위해 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)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
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)