简体   繁体   English

从MySQL中存储的数据中获取最后5周的时间

[英]Get last 5 weeks from data stored in MySQL

I have inherited an app which stores its attendance dates in MySQL database. 我继承了一个将出勤日期存储在MySQL数据库中的应用程序。 Dates as stored as periods for week starting Monday till Friday for each year. 每年从星期一到星期五从一周开始存储的日期。

The schema of the db is illustrated below: 数据库的架构如下所示:

PeriodYear | WeekNo |  WeekStartDate
2015       | 49     |  07-12-2015
2015       | 50     |  14-12-2015
2015       | 51     |  21-12-2015
2015       | 52     |  28-12-2015
2016       | 01     |  04-01-2016
2016       | 02     |  11-01-2016

I need to get last 4 weeks from MySQL. 我需要从MySQL获得最后4个星期。 The problem is if I do a select on current year (ie2016) I get only 1 week as week 52 is in year 2015. 问题是,如果我在当年(即2016年)进行选择,那么我只有1周,因为第52周在2015年。

How can I get last 4 weeks (ie week 49 - 2015, week 52 -2015, week 51 - 2015, week 50 - 2015)? 如何获得最近4周的时间(即,第49-2015周,第52-2015周,第51-2015周,第50-2015周)?

SELECT * FROM YOURTABLEHERE 
WHERE WeekStartDate BETWEEN (NOW() - INTERVAL 4 WEEK) AND NOW()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM