MySQL选择日期在30天范围内?
要选择30天范围内的日期,您可以使用算术运算-带间隔。
语法如下-
select *from yourTableNamewhere yourDateColumnName > NOW() - INTERVAL 30 DAY
and yourDateColumnName < NOW() + INTERVAL 30 DAY;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table selectDatesDemo-> (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> ArrivalDate datetime
-> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into selectDatesDemo(ArrivalDate) values('2019-01-10');mysql> insert into selectDatesDemo(ArrivalDate) values('2019-01-29');
mysql> insert into selectDatesDemo(ArrivalDate) values('2019-02-13');
mysql> insert into selectDatesDemo(ArrivalDate) values('2019-02-19');
mysql> insert into selectDatesDemo(ArrivalDate) values('2018-02-13');
mysql> insert into selectDatesDemo(ArrivalDate) values('2018-03-13');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from selectDatesDemo;
这是输出-
+----+---------------------+| Id | ArrivalDate |
+----+---------------------+
| 1 | 2019-01-10 00:00:00 |
| 2 | 2019-01-29 00:00:00 |
| 3 | 2019-02-13 00:00:00 |
| 4 | 2019-02-19 00:00:00 |
| 5 | 2018-02-13 00:00:00 |
| 6 | 2018-03-13 00:00:00 |
+----+---------------------+
6 rows in set (0.00 sec)
这是查询以选择30天范围内的日期-
mysql> select *from selectDatesDemo-> where ArrivalDate > NOW() - INTERVAL 30 DAY
-> and ArrivalDate < NOW() + INTERVAL 30 DAY;
以下是输出-
+----+---------------------+| Id | ArrivalDate |
+----+---------------------+
| 3 | 2019-02-13 00:00:00 |
| 4 | 2019-02-19 00:00:00 |
+----+---------------------+
2 rows in set (0.04 sec)
以上是 MySQL选择日期在30天范围内? 的全部内容, 来源链接: utcz.com/z/334764.html