时间:2021-07-01 10:21:17 帮助过:6人阅读
1.把当前时间转换成时间戳
select UNIX_TIMESTAMP();2.关于年龄我们一般不直接写具体的年龄,因为年龄是不断变化的,只需要在数据库中给出生日即可。sql语句如下。
select name,birth,year(curdate())-year(birth())-(right(curdate(),5)-right(birth,5)) as age from peg oreder by name;
以上sql 输出的图像如下是:
+----------+------------+------------+------+
| name | birth | CURDATE() | age |
+----------+------------+------------+------+
| Bowser | 1989-08-31 | 2003-08-19 | 13 |
| Buffy | 1989-05-13 | 2003-08-19 | 14 |
| Chirpy | 1998-09-11 | 2003-08-19 | 4 |
| Claws | 1994-03-17 | 2003-08-19 | 9 |
| Fang | 1990-08-27 | 2003-08-19 | 12 |
| Fluffy | 1993-02-04 | 2003-08-19 | 10 |
| Puffball | 1999-03-30 | 2003-08-19 | 4 |
| Slim | 1996-04-29 | 2003-08-19 | 7 |
| Whistler | 1997-12-09 | 2003-08-19 | 5 |
+----------+------------+------------+------+
3.如果要算出下个月出生的动物该sql语句如下。
语句一:
SELECT name, birth FROM pet WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));
语句二:
select name,birth from pet where month(birth)=mod(month(curdate(),12)+1;
为什么这样写是因为12月份特殊如果直接给它加上1的,那么就变成了13,所以这样处理。