码迷,mamicode.com
首页 > 数据库 > 详细

MySQL时间专题

时间:2016-04-12 15:57:34      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:

user_event :用户事件表

create_time :表中存储时间的字段

#获取当月数据
SELECT * FROM user_event WHERE DATE_FORMAT(create_time,%Y-%m) = DATE_FORMAT(NOW(),%Y-%m)

#获取3月份数据
SELECT * FROM user_event WHERE DATE_FORMAT(create_time,%Y-%m) = DATE_FORMAT(2016-03-01,%Y-%m)

#获取三月份数据
SELECT * FROM user_event WHERE YEAR(create_time)=2016 AND MONTH(create_time)=3

#获取本周数据
SELECT * FROM user_event WHERE YEARWEEK(DATE_FORMAT(create_time,%Y-%m-%d)) = YEARWEEK(NOW()); 

#查询上周的数据 
SELECT * FROM user_event WHERE YEARWEEK(DATE_FORMAT(create_time,%Y-%m-%d)) = YEARWEEK(NOW())-1; 

#查询距离当前现在6个月的数据 
SELECT * FROM user_event WHERE create_time BETWEEN DATE_SUB(NOW(),interval 6 month) and NOW();

#查询上个月的数据 
SELECT * FROM user_event WHERE DATE_FORMAT(create_time,%Y-%m)=DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH),%Y-%m)

以上语句整理自:http://www.jb51.net/article/32277.htm

#查询今天的信息记录:
SELECT * FROM user_event WHERE TO_DAYS(`create_time`) = TO_DAYS(NOW());

#查询昨天的信息记录:
SELECT * FROM user_event WHERE TO_DAYS(now()) - TO_DAYS(create_time) <= 1;

#查询近7天的信息记录:
SELECT * FROM user_event WHERE DATE_SUB(curdate(), INTERVAL 7 DAY) <= DATE(create_time);

#查询近30天的信息记录:
SELECT * FROM user_event WHERE DATE_SUB(curdate(), INTERVAL 30 DAY) <= DATE(create_time);

#查询上一月的信息记录:
SELECT * FROM user_event WHERE PERIOD_DIFF(DATE_FORMAT(NOW(), %Y%m), DATE_FORMAT(create_time, %Y%m)) =1; 

 以上语句整理自:http://www.oschina.net/code/snippet_583419_22850 

MySQL时间专题

标签:

原文地址:http://www.cnblogs.com/inkyi/p/5382868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!