获取学生出生日期记录范围之间日期的 MySQL 查询?
要获取日期之间的记录,请使用 BETWEEN。我们首先创建一个表 -
mysql> create table DemoTable863(StudentDateOfBirth date); Query OK, 0 rows affected (0.56 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable863 values('1998-01-10'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable863 values('2000-10-15'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable863 values('2003-04-20'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable863 values('2005-12-31'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable863 values('1999-07-01'); Query OK, 1 row affected (0.27 sec)
使用 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 到 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)
廣告