MySQL语句

时间相关

获取当前日期
select curdate();

获取当月最后一天
select last_day(curdate());

获取本月第一天
select DATE_ADD(curdate(),interval -day(curdate())+1 day);

获取下个月的第一天
select date_add(curdate()-day(curdate())+1,interval 1 month);

获取当前月的天数
select DATEDIFF(date_add(curdate()-day(curdate())+1,interval 1 month ),DATE_ADD(curdate(),interval -day(curdate())+1 day)) from dual;

之前到今天零点的所有数据
SELECT count(*),code from verification_code where TO_DAYS(now()) > TO_DAYS(time);

与昨天在同一月的所有数据
SELECT * from verification_code WHERE TO_DAYS(now()) > TO_DAYS(time) and DATE_FORMAT(time,’%Y-%m’)=DATE_FORMAT(DATE_SUB(curdate(),INTERVAL 1 day),’%Y-%m’);

与昨天在同一周的所有数据(昨天的这一周的周一开始算起)
SELECT * from verification_code WHERE TO_DAYS(now()) > TO_DAYS(time)
AND YEARWEEK(date_format(time,’%Y-%m-%d’)) = YEARWEEK(DATE_SUB(curdate(),INTERVAL 1 day));

查询距离当前现在6个月的数据
select NewsCreateDate from web_news where NewsCreateDate between date_sub(now(),interval 6 month) and now()

查询当前这周的数据
select NewsCreateDate from web_news where YEARWEEK(date_format(NewsCreateDate,’%Y-%m-%d’)) = YEARWEEK(now())

查询上周的数据
select NewsCreateDate from web_news where YEARWEEK(date_format(NewsCreateDate,’%Y-%m-%d’)) = YEARWEEK(now())-1

查询当天的数据
select NewsCreateDate from web_news where date(date_format(NewsCreateDate,’%Y-%m-%d’))=date(now())

查询当前月份的数据
select NewsCreateDate from web_news where date_format(NewsCreateDate,’%Y-%m’)=date_format(now(),’%Y-%m’)

查询上个月的数据
select NewsCreateDate from web_news where date_format(NewsCreateDate,’%Y-%m’)=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),’%Y-%m’)

你可能感兴趣的:(mysql)