发布网友 发布时间:2022-04-20 13:54
共2个回答
懂视网 时间:2022-04-30 16:38
感谢:http://www.jb51.net/article/32277.htm
------------------------------------------------------------------------------
MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查询本周、上周、本月、上个月份的数据,如果您对MySQL查询方面感兴趣的话,不妨一看
查询当前这周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,‘%Y-%m-%d‘)) = YEARWEEK(now());
查询上周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,‘%Y-%m-%d‘)) = YEARWEEK(now())-1;
查询当前月份的数据
select name,submittime from enterprise where date_format(submittime,‘%Y-%m‘)=date_format(now(),‘%Y-%m‘)
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查询上个月的数据
select name,submittime from enterprise where date_format(submittime,‘%Y-%m‘)=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),‘%Y-%m‘)
select * from `user` where DATE_FORMAT(pudate,‘%Y%m‘) = DATE_FORMAT(CURDATE(),‘%Y%m‘) ;
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,‘%y-%m-%d‘)) = WEEKOFYEAR(now())
select *
from user
where MONTH(FROM_UNIXTIME(pudate,‘%y-%m-%d‘)) = MONTH(now())
select *
from [user]
where YEAR(FROM_UNIXTIME(pudate,‘%y-%m-%d‘)) = YEAR(now())
and MONTH(FROM_UNIXTIME(pudate,‘%y-%m-%d‘)) = MONTH(now())
select *
from [user]
where pudate between 上月最后一天
and 下月第一天
MySQL查询本周、上周、本月、上个月份数据的sql代码(转)
标签:
热心网友 时间:2022-04-30 13:46
首先要考虑给数据库减压,意思就是让数据库做最简单的事情。你可以把条件在php里边组装好,然后让mysql只单一的执行查询就好了,php的时间代码给你贴一下吧
';
//php获取今日开始时间戳和结束时间戳?
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
echo?"今日开始时间戳和结束时间戳",'开始:',$beginToday,'结束:',$endToday,'
';
echo?"今日开始时间:",date("Y-m-d?H:i:s",$beginToday),'
';
echo?"今日结束时间:",date("Y-m-d?H:i:s",$endToday),'
';
echo?'';
//php获取昨日起始时间戳和结束时间戳
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;?
echo?"昨日开始时间戳和结束时间戳",'开始:',$beginYesterday,'结束:',$endYesterday,'
';
echo?"昨日开始时间:",date("Y-m-d?H:i:s",$beginYesterday),'
';
echo?"昨日结束时间:",date("Y-m-d?H:i:s",$endYesterday),'
';
echo?'';
//php获取上周起始时间戳和结束时间戳
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
echo?"上周开始时间戳和结束时间戳",'开始:',$beginLastweek,'结束:',$endLastweek,'
';
echo?"上周开始时间:",date("Y-m-d?H:i:s",$beginLastweek),'
';
echo?"上周结束时间:",date("Y-m-d?H:i:s",$endLastweek),'
';
echo?'';
//php获取本月起始时间戳和结束时间戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
echo?"本月开始时间戳和结束时间戳",'开始:',$beginThismonth,'结束:',$endThismonth,'
';
echo?"本月开始时间:",date("Y-m-d?H:i:s",$beginThismonth),'
';
echo?"本月结束时间:",date("Y-m-d?H:i:s",$endThismonth),'
';
?>