MySQL选择日期在30天范围内?
要选择30天范围内的日期,您可以使用算术运算-带间隔。
语法如下-
select *from yourTableName where 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)
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿