如何有条件地在MySQL中的日期之间/之前/之后选择?
以下是语法-
select *from yourTableNamewhere
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
−> );
借助insert命令将一些记录插入表中-
mysql> insert into demo35(joining_date,relieving_date) values('2020−01−10','2020−07−11');mysql> insert into demo35(joining_date,relieving_date) values('2020−05−07','2020−12−08');
mysql> insert into demo35(joining_date,relieving_date) values('2020−04−11','2020−09−18');
mysql> insert into demo35(joining_date,relieving_date) values('2020−03−12','2020−10−01');
使用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)
以上是 如何有条件地在MySQL中的日期之间/之前/之后选择? 的全部内容, 来源链接: utcz.com/z/326464.html