要在日期之间获取记录,请使用BETWEEN。让我们首先创建一个表-
mysql> create table DemoTable863(StudentDateOfBirth date);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable863 values('1998-01-10'); mysql> insert into DemoTable863 values('2000-10-15'); mysql> insert into DemoTable863 values('2003-04-20'); mysql> insert into DemoTable863 values('2005-12-31'); mysql> insert into DemoTable863 values('1999-07-01');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable863;
这将产生以下输出-
+--------------------+ | StudentDateOfBirth | +--------------------+ | 1998-01-10 | | 2000-10-15 | | 2003-04-20 | | 2005-12-31 | | 1999-07-01 | +--------------------+ 5 rows in set (0.00 sec)
以下是查询以显示基于DOB的范围内的日期,即20 AND 21-
mysql> select *from DemoTable863 where (YEAR(NOW()) - YEAR(StudentDateOfBirth)) BETWEEN 20 AND 21;
这将产生以下输出-
+--------------------+ | StudentDateOfBirth | +--------------------+ | 1998-01-10 | | 1999-07-01 | +--------------------+ 2 rows in set (0.03 sec)