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

mysql时间函数总结_MySQL

WBOY
Release: 2016-06-01 13:17:14
Original
903 people have browsed it

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

1.把当前时间转换成时间戳

select  UNIX_TIMESTAMP();
Copy after login
2.关于年龄我们一般不直接写具体的年龄,因为年龄是不断变化的,只需要在数据库中给出生日即可。sql语句如下。
select name,birth,year(curdate())-year(birth())-(right(curdate(),5)-right(birth,5)) as age from peg oreder by name;
Copy after login

以上sql 输出的图像如下是:

+----------+------------+------------+------+
Copy after login
Copy after login
Copy after login
| name     | birth      | CURDATE()  | age  |
Copy after login
+----------+------------+------------+------+
Copy after login
Copy after login
Copy after login
| Bowser   | 1989-08-31 | 2003-08-19 |   13 |
Copy after login
| Buffy    | 1989-05-13 | 2003-08-19 |   14 |
Copy after login
| Chirpy   | 1998-09-11 | 2003-08-19 |    4 |
Copy after login
| Claws    | 1994-03-17 | 2003-08-19 |    9 |
Copy after login
| Fang     | 1990-08-27 | 2003-08-19 |   12 |
Copy after login
| Fluffy   | 1993-02-04 | 2003-08-19 |   10 |
Copy after login
| Puffball | 1999-03-30 | 2003-08-19 |    4 |
Copy after login
| Slim     | 1996-04-29 | 2003-08-19 |    7 |
Copy after login
| Whistler | 1997-12-09 | 2003-08-19 |    5 |
Copy after login
+----------+------------+------------+------+
Copy after login
Copy after login
Copy after login
3.如果要算出下个月出生的动物该sql语句如下。
Copy after login
语句一:
Copy after login
SELECT name, birth FROM pet WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));
Copy after login
语句二:
Copy after login
select name,birth from pet where month(birth)=mod(month(curdate(),12)+1;
Copy after login
为什么这样写是因为12月份特殊如果直接给它加上1的,那么就变成了13,所以这样处理。
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template