[英]Select data from last week mysql query
I have a table with a datefield and I want to select all the entries that came from the last week,Saturday to Sunday(not last 7 days). 我有一个带有日期字段的表,我想选择从上周(星期六)到周日(不是过去7天)的所有条目。
Table values 表值
Date ID Value
8/3/2015 8:03 PM a 10
8/4/2015 8:03 PM b 20
8/5/2015 8:03 PM c 30
8/6/2015 8:03 PM d 40
8/7/2015 8:03 PM e 50
8/9/2015 8:03 PM f 60
8/10/2015 8:03 PM g 70
I only want to select id from last which are a,b,c,d,e. 我只想从最后选择a,b,c,d,e的ID。 How do i write the mysql query in this ?
我该如何在其中编写mysql查询?
This is a bit tricky. 这有点棘手。 One method is to try something like
week()
, but that has a problem at the beginning of the year. 一种方法是尝试使用诸如
week()
,但是这种方法在年初会出现问题。
Another approach is to get the date of midnight on Sunday and use that for the calculation. 另一种方法是获取星期日的午夜日期并将其用于计算。 I think this expression does that:
我认为此表达式可以做到:
date_sub(curdate(), interval (weekday(curdate()) + 1) % 7) - 1 day)
You can then plug this into the where
clause: 然后可以将其插入
where
子句:
select v.*
from values v
where v.date < date_sub(curdate(), interval (weekday(curdate()) + 1) % 7) - 1 day) and
v.date >= date_sub(curdate(), interval (weekday(curdate()) + 1) % 7) - 8 day);
Note: this formulation can make use of an index on date
. 注意:此公式可以使用
date
上的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.