如何在 MySQL 中根据条件选择介于/之前/之后的日期?
语法如下 −
select *from yourTableName where yourColumnName1 < yourValue1 AND (yourColumnName2 > yourValue2 OR yourColumnName2 is null);
让我们创建一个表 −
mysql> create table demo35 −> ( −> id int NOT NULL AUTO_INCREMENT PRIMARY KEY, −> joining_date date, −> relieving_date date −> ); Query OK, 0 rows affected (3.88 sec)
使用插入命令向表中插入一些记录 −
mysql> insert into demo35(joining_date,relieving_date) values('2020−01−10','2020−07−11'); Query OK, 1 row affected (0.15 sec) mysql> insert into demo35(joining_date,relieving_date) values('2020−05−07','2020−12−08'); Query OK, 1 row affected (0.17 sec) mysql> insert into demo35(joining_date,relieving_date) values('2020−04−11','2020−09−18'); Query OK, 1 row affected (0.14 sec) mysql> insert into demo35(joining_date,relieving_date) values('2020−03−12','2020−10−01'); Query OK, 1 row affected (0.13 sec)
使用 select 语句从表中显示记录 −
mysql> select *from demo35;
将生成以下输出 −
+----+--------------+----------------+ | id | joining_date | relieving_date | +----+--------------+----------------+ | 1 | 2020−01−10 | 2020−07−11 | | 2 | 2020−05−07 | 2020−12−08 | | 3 | 2020−04−11 | 2020−09−18 | | 4 | 2020−03−12 | 2020−10−01 | +----+--------------+----------------+ 4 rows in set (0.00 sec)
以下是 MySQL 中根据条件选择日期的查询 −
mysql> select *from demo35 −> where −> joining_date < '2020−05−11' AND −> (relieving_date > '2020−08−10' OR relieving_date is null);
将生成以下输出 −
+----+--------------+----------------+ | id | joining_date | relieving_date | +----+--------------+----------------+ | 2 | 2020−05−07 | 2020−12−08 | | 3 | 2020−04−11 | 2020−09−18 | | 4 | 2020−03−12 | 2020−10−01 | +----+--------------+----------------+ 3 rows in set (0.00 sec)
广告