-- 假设条件日期为2018-01-19, 以下列举取上周始末和周几
select date_add(20180119, interval -7 - weekday(20180119) day) as last_week_start,
date_add(20180119, interval -1 - weekday(20180119) day) as last_week_end,
date_add(20180119, interval -8 - weekday(20180119) + 1 day) as last_week_monday,
date_add(20180119, interval -8 - weekday(20180119) + 2 day) as last_week_tuesday,
-- ...
date_add(20180119, interval -8 - weekday(20180119) + 7 day) as last_week_sunday
-- 取上周所有, 如果creationDate时间部分为00:00:00.0 可以直接小于等于上述last_week_end
select 1
from student
where creationDate >= date_add(20180119, interval -7 - weekday(20180119) day)
and creationDate < date_add(20180119, interval - weekday(20180119) day)
-- 取上周二, 如果creationDate时间部分为00:00:00.0 可以直接用一个等于条件
select 1
from student
where creationDate >= date_add(20180119, interval -8 - weekday(20180119) + 2 day)
and creationDate < date_add(20180119, interval -8 - weekday(20180119) + 3 day)
首先要考虑给数据库减压,意思就是让数据库做最简单的事情。你可以把条件在php里边组装好,然后让mysql只单一的执行查询就好了,php的时间代码给你贴一下吧
<?phpdate_default_timezone_set('Asia/Shanghai')
echo "今天:".date("Y-m-d H:i:s")."<br>"
echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>"
echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>"
echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>"
echo "一周前:".date("Y-m-d",strtotime("-1 week")). "<br>"
echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>"
echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>"
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>"
echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>"
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>"
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>"
echo '<hr/>'
//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,'<br/>'
echo "今日开始时间:",date("Y-m-d H:i:s",$beginToday),'<br/>'
echo "今日结束时间:",date("Y-m-d H:i:s",$endToday),'<br/>'
echo '<hr/>'
//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,'<br/>'
echo "昨日开始时间:",date("Y-m-d H:i:s",$beginYesterday),'<br/>'
echo "昨日结束时间:",date("Y-m-d H:i:s",$endYesterday),'<br/>'
echo '<hr/>'
//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,'<br/>'
echo "上周开始时间:",date("Y-m-d H:i:s",$beginLastweek),'<br/>'
echo "上周结束时间:",date("Y-m-d H:i:s",$endLastweek),'<br/>'
echo '<hr/>'
//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,'<br/>'
echo "本月开始时间:",date("Y-m-d H:i:s",$beginThismonth),'<br/>'
echo "本月结束时间:",date("Y-m-d H:i:s",$endThismonth),'<br/>'
?>
select a.name,count(*)from (
select *
from 表
where onduty = no) a
group by name
having count(*) = 5
原理说明:
1.取出所有缺勤记录作为a表
2.取出a表中缺勤次数为5的(因为没有重复记录不会1个人1个礼拜出现5次以上的出勤情况)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)