Mysql常用时间函数

880阅读 0评论2014-07-09 lolizeppelin
分类:Mysql/postgreSQL

有些函数偶尔用网上翻个半天还找不到,记录下来

时间转日期(或者说是格式化日期显示函数)
date_format(logindate,'%Y/%m/%d')
把data字段显示为2014/01/01这样的格式方便统计,用例获取每个用户登陆日期,从而可以计算用户到底有多少天登陆过
select userid,date_format(logindate,'%Y/%m/%d'),count(date_format(logindate,'%Y/%m/%d')) from last_login group by userid,date_format(logindate,'%Y/%m/%d')

datetime(或datatime格式的string)转unix时间——unix_timestamp(datetime),
用例unix_timestamp('2014-01-31'),unix_timestamp('2014/01/31'),unix_timestamp('2014-01-01 00:00:00')

unix时间转datetime——from_unixtime()
用例from_unixtime(t_time),from_unixtime(t_time,'%Y/%m/%d')
上一篇:ssh远程命令无发退出的原因
下一篇:Mysql去除完全重复行语句