두 날짜 범위 사이의 모든 데이터를 선택하려면 MySQL BETWEEN −
를 사용하십시오.select * from yourTableName where yourColumnName between yourDateValue1 and yourDateValue2;
먼저 −
를 생성해 보겠습니다.mysql> create table DemoTable1422 -> ( -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EmployeeName varchar(20), -> EmployeeJoiningDate date -> ); Query OK, 0 rows affected (0.69 sec)
insert −
를 사용하여 테이블에 일부 레코드 삽입mysql> insert into DemoTable1422(EmployeeName,EmployeeJoiningDate) values('John','2017-09-28'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable1422(EmployeeName,EmployeeJoiningDate) values('Chris','2019-09-29'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1422(EmployeeName,EmployeeJoiningDate) values('Bob','2018-01-31'); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable1422(EmployeeName,EmployeeJoiningDate) values('Sam','2018-12-21'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1422(EmployeeName,EmployeeJoiningDate) values('Mike','2019-11-10'); Query OK, 1 row affected (0.11 sec)
select −
를 사용하여 테이블의 모든 레코드 표시mysql> select * from DemoTable1422;
이것은 다음과 같은 출력을 생성합니다 -
+------------+--------------+---------------------+ | EmployeeId | EmployeeName | EmployeeJoiningDate | +------------+--------------+---------------------+ | 1 | John | 2017-09-28 | | 2 | Chris | 2019-09-29 | | 3 | Bob | 2018-01-31 | | 4 | Sam | 2018-12-21 | | 5 | Mike | 2019-11-10 | +------------+--------------+---------------------+ 5 rows in set (0.00 sec)
다음은 두 날짜 범위 사이의 모든 데이터를 선택하는 쿼리입니다 -
mysql> select * from DemoTable1422 where EmployeeJoiningDate between '2018-09-29' and '2019-09-29';
이것은 다음과 같은 출력을 생성합니다 -
+------------+--------------+---------------------+ | EmployeeId | EmployeeName | EmployeeJoiningDate | +------------+--------------+---------------------+ | 2 | Chris | 2019-09-29 | | 4 | Sam | 2018-12-21 | +------------+--------------+---------------------+ 2 rows in set (0.00 sec)