从 MySQL 中的 DOB 查询年龄?
我们先创建一个表 -
mysql> create table DemoTable611 (DOB date); Query OK, 0 rows affected (0.99 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable611 values('1996-04-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable611 values('2001-01-31'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable611 values('2004-12-21'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable611 values('2000-02-03'); Query OK, 1 row affected (0.14 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable611;
这将生成以下输出 -
+------------+ | DOB | +------------+ | 1996-04-21 | | 2001-01-31 | | 2004-12-21 | | 2000-02-03 | +------------+ 4 rows in set (0.00 sec)
从 DOB 查询年龄 -
mysql> select *from DemoTable611 where DOB >= (CURDATE() - INTERVAL 16 YEAR);
这将生成以下输出 -
+------------+ | DOB | +------------+ | 2004-12-21 | +------------+ 1 row in set (0.00 sec)
广告