[英]MySQL Query to select data from last week?
Hi I have a table with a date field and some other information.嗨,我有一个带有日期字段和其他一些信息的表格。 I want to select all entries from the past week, (week start from Sunday).
我想选择过去一周的所有条目(从星期日开始的一周)。
table values:表值:
id date
2 2011-05-14 09:17:25
5 2011-05-16 09:17:25
6 2011-05-17 09:17:25
8 2011-05-20 09:17:25
15 2011-05-22 09:17:25
I want to select all ids from last week, expected output is 5, 6, 8. (id 2 not in last week, and id 15 is in current week.)我想选择上周的所有 id,预期输出为 5、6、8。 (id 2 不在上周,id 15 在本周。)
How to write and SQL Query for the same.怎么写和SQL Query一样。
select id from tbname
where date between date_sub(now(),INTERVAL 1 WEEK) and now();
SELECT id FROM tbl
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
SELECT id FROM table1
WHERE YEARWEEK(date) = YEARWEEK(NOW() - INTERVAL 1 WEEK)
I use the YEARWEEK function specifically to go back to the prior whole calendar week (as opposed to 7 days before today).我专门使用 YEARWEEK 函数返回到之前的整个日历周(而不是今天之前的 7 天)。 YEARWEEK also allows a second argument that will set the start of the week or determine how the first/last week of the year are handled.
YEARWEEK 还允许设置一周的开始或确定如何处理一年的第一周/最后一周的第二个参数。 YEARWEEK lets you to keep the number of weeks to go back/forward in a single variable, and will not include the same week number from prior/future years, and it's far shorter than most of the other answers on here.
YEARWEEK 允许您将要返回/向前的周数保留在单个变量中,并且不会包含前几年/未来几年的相同周数,并且它比此处的大多数其他答案要短得多。
Simplified form:简化形式:
Last week data:上周数据:
SELECT id FROM tbl
WHERE
WEEK (date) = WEEK( current_date ) - 1 AND YEAR( date) = YEAR( current_date );
2 weeks ago data: 2周前数据:
SELECT id FROM tbl
WHERE
WEEK (date) = WEEK( current_date ) - 2 AND YEAR( date) = YEAR( current_date );
SQL Fiddle SQL小提琴
http://sqlfiddle.com/#!8/6fa6e/2 http://sqlfiddle.com/#!8/6fa6e/2
You can make your calculation in php and then add it to your query:您可以在 php 中进行计算,然后将其添加到您的查询中:
$date = date('Y-m-d H:i:s',time()-(7*86400)); // 7 days ago
$sql = "SELECT * FROM table WHERE date <='$date' ";
now this will give the date for a week ago现在这将给出一周前的日期
PLEASE people... 'Last week' like the OP asked and where I was looking for (but found none of answers satisfying) is THE LAST WEEK.请人们......像OP所问的“上周”以及我正在寻找的地方(但没有找到令人满意的答案)是最后一周。
If today is Tuesday, then LAST WEEK is Monday A WEEK AGO to Sunday A WEEK AGO .如果今天是星期二,那么LAST WEEK是从 Monday A WEEK A GO到 Sunday A WEEK A GO 。
So:所以:
WHERE
WEEK(yourdate) = WEEK(NOW()) - 1
Or for ISO weeks:或 ISO 周:
WHERE
WEEK(yourdate, 3) = WEEK(NOW(), 3) - 1
Last Week上个星期
SELECT UserName, InsertTime
FROM tblaccounts
WHERE WEEK(InsertTime) = WEEK(NOW()) - 1;
Last Month上个月
SELECT UserName, InsertTime
FROM tblaccounts
WHERE MONTH(InsertTime) = MONTH(NOW()) - 1;
Last YEAR去年
SELECT UserName, InsertTime
FROM tblaccounts
WHERE YEAR(InsertTime) = YEAR(NOW()) - 1;
Probably the most simple way would be:可能最简单的方法是:
SELECT id
FROM table
WHERE date >= current_date - 7
For 8 days (ie Monday - Monday) 8天(即周一-周一)
它可以在一行中:
SELECT * FROM table WHERE Date BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
You'll need to calc which day relative to today is Sunday in your middleware (php, python, etc.)*您需要在中间件(php、python 等)中计算相对于今天的哪一天是星期日*
Then,然后,
select id
from table
where date >= "$sunday-date" + interval 7 DAY
一种简单的方法可以是这种方法,这是我的代码中的一个真实示例,并且可以完美运行:
where("actions.created_at >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK)")
The above query will not work.上面的查询将不起作用。 After the
where
clause, if we can not CAST
the column value, then it will not work.在
where
子句之后,如果我们不能CAST
列值,那么它将不起作用。 You should cast
the column value.你应该
cast
的列值。
eg:例如:
SELECT.....
WHERE CAST( yourDateColumn AS DATE ) > DATEADD( DAY, -7, CAST( GETDATE() AS DATE )
For more example Like last month, last year, last 15 days, last 3 months 更多示例如上个月、去年、过去 15 天、过去 3 个月
Fetch Last WEEK Record获取上周记录
Using the below MySQL query for fetching the last week records from the mysql database table.使用下面的 MySQL 查询从 mysql 数据库表中获取上周的记录。
SELECT name, created_at
FROM employees
WHERE
YEARWEEK(`created_at`, 1) = YEARWEEK( CURDATE() - INTERVAL 1 WEEK, 1)
如果您要检索过去 7 天内的记录,可以使用以下代码段:
SELECT date FROM table_name WHERE DATE(date) >= CURDATE() - INTERVAL 7 DAY;
SELECT id FROM tb1
WHERE
YEARWEEK (date) = YEARWEEK( current_date -interval 1 week )
I often do a quick "last week" check as well and the following tends to work well for me and includes today.我也经常做一个快速的“上周”检查,以下内容对我来说效果很好,包括今天。
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = Getdate() - 7 /* Seven Days Earlier */
SET @EndDate = Getdate() /* Now */
SELECT id
FROM mytable
WHERE date BETWEEN @StartDate AND @Enddate
If you want this to NOT include today just subtract an extra day from the @EndDate.如果您希望这不包括今天,只需从@EndDate 中减去一天。 If I select these two variables today get
如果我今天选择这两个变量得到
@StartDate 2015-11-16 16:34:05.347 /* Last Monday */
@StartDate 2015-11-16 16:34:05.347 /* 上周一 */
@EndDate 2015-11-23 16:34:05.347 /* This Monday */
@EndDate 2015-11-23 16:34:05.347 /* 这个星期一 */
If I wanted Sunday to Sunday I would have the following.如果我想要周日到周日,我会有以下内容。
SET @StartDate = Getdate() - 8 /* Eight Days Earlier */
SET @EndDate = Getdate() - 1 /* Yesterday */
@StartDate 2015-11-15 16:34:05.347 /* Previous Sunday */
@StartDate 2015-11-15 16:34:05.347 /* 上一个星期天 */
@EndDate 2015-11-22 16:34:05.347 /* Last Sunday */
@EndDate 2015-11-22 16:34:05.347 /* 上周日 */
WHERE yourDateColumn > DATEADD(DAY, -7, GETDATE()) ;
You can also use it esay way你也可以用它esay方式
SELECT *
FROM inventory
WHERE YEARWEEK(`modify`, 1) = YEARWEEK(CURDATE(), 1)
i Use this for the week start from SUNDAY:我在从星期日开始的一周使用它:
SELECT id FROM tbl
WHERE
date >= curdate() - INTERVAL DAYOFWEEK(curdate())+5 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-2 DAY
Try this:尝试这个:
Declare @Daytype varchar(15),
@StartDate datetime,
@EndDate datetime
set @Daytype = datename(dw, getdate())
if @Daytype= 'Monday'
begin
set @StartDate = getdate()-7
set @EndDate = getdate()-1
end
else if @Daytype = 'Tuesday'
begin
set @StartDate = getdate()-8
set @EndDate = getdate()-2
end
Else if @Daytype = 'Wednesday'
begin
set @StartDate = getdate()-9
set @EndDate = getdate()-3
end
Else if @Daytype = 'Thursday'
begin
set @StartDate = getdate()-10
set @EndDate = getdate()-4
end
Else if @Daytype = 'Friday'
begin
set @StartDate = getdate()-11
set @EndDate = getdate()-5
end
Else if @Daytype = 'Saturday'
begin
set @StartDate = getdate()-12
set @EndDate = getdate()-6
end
Else if @Daytype = 'Sunday'
begin
set @StartDate = getdate()-13
set @EndDate = getdate()-7
end
select @StartDate,@EndDate
You can try this one.你可以试试这个。 it worked for me :
它对我有用:
where date(createdtime) <= date(curdate())-7
In the the above code createdtime
is database field name, as individuals this name could vary.在上面的代码中,
createdtime
是数据库字段名称,这个名称可能因人而异。
If you already know the dates then you can simply use between, like this:如果您已经知道日期,那么您可以简单地使用 between,如下所示:
SELECT id
FROM `Mytable`
where MyDate BETWEEN "2011-05-15" AND "2011-05-21"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.