要选择今天的日期在两个日期列之间的MySQL行,您需要使用AND运算符。语法如下:
SELECT *FROM yourTableName WHERE yourDateColumnName1 <=’yourDateValue’ AND yourDateColumnName2 >= ‘’yourDateValue’;
为了理解上述语法,让我们创建一个表。创建表的查询如下:
mysql> create table selectDates -> ( -> Id int NOT NULL AUTO_INCREMENT, -> StartingDate date, -> EndingDate date, -> PRIMARY KEY(Id) -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下:
mysql> insert into selectDates(StartingDate,EndingDate) values('2019-01-11','2019-01-23'); mysql> insert into selectDates(StartingDate,EndingDate) values('2019-01-10','2019-01-23'); mysql> insert into selectDates(StartingDate,EndingDate) values('2019-01-30','2019-01-30'); mysql> insert into selectDates(StartingDate,EndingDate) values('2019-10-14','2019-10-28'); mysql> insert into selectDates(StartingDate,EndingDate) values('2019-10-14','2019-10-20'); mysql> insert into selectDates(StartingDate,EndingDate) values('2019-11-17','2019-11-19'); mysql> insert into selectDates(StartingDate,EndingDate) values('2019-12-21','2019-12-31'); mysql> insert into selectDates(StartingDate,EndingDate) values('2019-01-06','2019-01-21'); mysql> insert into selectDates(StartingDate,EndingDate) values('2019-01-07','2019-01-17');
使用select语句显示表中的所有记录。查询如下:
mysql> select *from selectDates;
以下是输出:
+----+--------------+------------+ | Id | StartingDate | EndingDate | +----+--------------+------------+ | 1 | 2019-01-11 | 2019-01-23 | | 2 | 2019-01-10 | 2019-01-23 | | 3 | 2019-01-30 | 2019-01-30 | | 4 | 2019-10-14 | 2019-10-28 | | 5 | 2019-10-14 | 2019-10-20 | | 6 | 2019-11-17 | 2019-11-19 | | 7 | 2019-12-21 | 2019-12-31 | | 8 | 2019-01-06 | 2019-01-21 | | 9 | 2019-01-07 | 2019-01-17 | +----+--------------+------------+ 9 rows in set (0.00 sec)
这是在两个日期列之间选择今天日期的查询:
select *from selectDates where StartingDate <='2019-01-10' AND EndingDate >='2019-01-10';
以下是输出:
+----+--------------+------------+ | Id | StartingDate | EndingDate | +----+--------------+------------+ | 2 | 2019-01-10 | 2019-01-23 | | 8 | 2019-01-06 | 2019-01-21 | | 9 | 2019-01-07 | 2019-01-17 | +----+--------------+------------+ 3 rows in set (0.00 sec)