---
title: "MySQL时间函数"
author: "greybeard"
date: "2012-04-28T17:04:35Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2012-04-28T17:39:04Z"
id: 1503810
url: "https://greybeard.iteye.com/blog/1503810"
---

基本函数

当前日期: curdate()   
当前时间（包括日期): now()


格式化时间: date_format()


时间差：

TIMESTAMPDIFF
具体用法
TIMESTAMPDIFF(MINUTE, time1, time2)     返回相差的分钟
timestampdiff(week,'2012-04-28','2012-05-12') 周数，是根据相差天数来计算的，不是根据time1是周几，time2是周几计算，所以局限性很大
TIMESTAMPDIFF(day, MinDate, MaxDate)
TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01')  返回相差的月份
TIMESTAMPDIFF(YEAR,'2009-05-01','2008-01-01')   返回下查的年数

字符串函数:
concat     
连接函数：
具体用法：
select concat('1', '@', 'hello', 'jack' .....)





经典应用:

取到当前周一的日期：   subdate(curdate(),date_format(curdate(),'%w')-1)

格式化当前时间: date_format(curdate(), '%Y-%m-%d')