WHERE 절과 함께 OR 조건을 사용해야 합니다. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, MonthNumber int, YearNumber int ); Query OK, 0 rows affected (0.22 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable(MonthNumber,YearNumber) values(11,2018); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(MonthNumber,YearNumber) values(3,2019); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(MonthNumber,YearNumber) values(12,2018); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(MonthNumber,YearNumber) values(1,2019); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(MonthNumber,YearNumber) values(12,2019); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(MonthNumber,YearNumber) values(6,2018); Query OK, 1 row affected (0.12 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select *from DemoTable;
이것은 다음과 같은 출력을 생성합니다 -
+----+-------------+------------+ | Id | MonthNumber | YearNumber | +----+-------------+------------+ | 1 | 11 | 2018 | | 2 | 3 | 2019 | | 3 | 12 | 2018 | | 4 | 1 | 2019 | | 5 | 12 | 2019 | | 6 | 6 | 2018 | +----+-------------+------------+ 6 rows in set (0.00 sec)
다음은 월별, 연도별 결과를 구하는 쿼리입니다. -
mysql> select Id,MonthNumber,YearNumber from DemoTable where (MonthNumber >= 11 AND YearNumber = 2019) OR (MonthNumber <=11 AND YearNumber = 2018);
이것은 다음과 같은 출력을 생성합니다 -
+----+-------------+------------+ | Id | MonthNumber | YearNumber | +----+-------------+------------+ | 1 | 11 | 2018 | | 5 | 12 | 2019 | | 6 | 6 | 2018 | +----+-------------+------------+ 3 rows in set (0.00 sec)