Home >Database >Mysql Tutorial >mysql时间函数总结_MySQL

mysql时间函数总结_MySQL

WBOY
WBOYOriginal
2016-06-01 13:17:14910browse

mysql 有一些时间函数,虽然不常用,但是有时面试时会用到。

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,所以这样处理。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn