MySQL 查询选择上周的数据?
您好,我有一个带有日期字段和一些其他信息的表格。 我想选择过去一周的所有条目(一周从周日开始)。
表值:
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
我想选择上周的所有 id,预期输出为 5、6、8。 (id 2 不在上周,id 15 在本周。)
如何编写相同的 SQL 查询。
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.)
How to write and SQL Query for the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
我专门使用 YEARWEEK 函数返回到之前的整个日历周(而不是今天之前的 7 天)。 YEARWEEK 还允许使用第二个参数来设置一周的开始或确定如何处理一年中的第一周/最后一周。 YEARWEEK 允许您将回退/前进的周数保留在单个变量中,并且不会包含前/未来年份的相同周数,并且它比此处的大多数其他答案要短得多。
I use the YEARWEEK function specifically to go back to the prior whole calendar week (as opposed to 7 days before today). 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 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.
简化形式:
上周数据:
2周前数据:
SQL Fiddle
http://sqlfiddle.com/#!8/6fa6e/2
Simplified form:
Last week data:
2 weeks ago data:
SQL Fiddle
http://sqlfiddle.com/#!8/6fa6e/2
如果您要检索过去 7 天内的记录,可以使用以下代码片段:
If you're looking to retrieve records within the last 7 days, you can use the snippet below:
您可以在 php 中进行计算,然后将其添加到您的查询中:
现在这将给出一周前的日期
You can make your calculation in php and then add it to your query:
now this will give the date for a week ago
也许最简单的方法是:
8 天(即星期一 - 星期一)
Probably the most simple way would be:
For 8 days (i.e. Monday - Monday)
请大家...“上周”就像OP问的那样,我在哪里寻找(但没有找到令人满意的答案)是最后一周。
如果今天是星期二,则最后一周是星期一一周前到星期日一周前。
所以:
或者对于 ISO 周:
PLEASE people... 'Last week' like the OP asked and where I was looking for (but found none of answers satisfying) is THE LAST WEEK.
If today is Tuesday, then LAST WEEK is Monday A WEEK AGO to Sunday A WEEK AGO.
So:
Or for ISO weeks:
这是在 MySQL 中获取上周、月份和年份记录的方法。
上周
上个月
去年
Here is a way to get last week, month, and year records in MySQL.
Last Week
Last Month
Last YEAR
您需要在中间件(php、python 等)中计算相对于今天的哪一天是星期日*
然后,
You'll need to calc which day relative to today is Sunday in your middleware (php, python, etc.)*
Then,
它可以在一行中:
It can be in a single line:
一种简单的方法可以是这个,这是我的代码中的一个真实示例,并且效果很好:
A simple way can be this one, this is a real example from my code and works perfectly:
上面的查询将不起作用。
在
where
子句之后,如果我们不能对列值进行CAST
,那么它将不起作用。您应该强制转换
列值。例如:
The above query will not work.
After the
where
clause, if we can notCAST
the column value, then it will not work. You shouldcast
the column value.e.g.:
我也经常做一个快速的“上周”检查,以下内容对我来说效果很好,包括今天。
如果您希望不包括今天,只需从@EndDate 中减去额外的一天即可。如果我今天选择这两个变量得到
如果我想要周日到周日,我会有以下内容。
I often do a quick "last week" check as well and the following tends to work well for me and includes today.
If you want this to NOT include today just subtract an extra day from the @EndDate. If I select these two variables today get
If I wanted Sunday to Sunday I would have the following.
你也可以用简单的方式使用它
You can also use it esay way
我从周日开始的一周使用这个:
i Use this for the week start from SUNDAY:
试试这个
-5 是从系统日期回溯 5 天,-12 是从系统日期回溯 12 天,对于此示例,星期三/或星期三到星期三无法回忆。
try this
-5 is 5 days back from system date ,, -12 is 12 days back from system date for this example wednesday / or sednesday to wednesday cant recall.
我的方式:
My way:
试试这个:
Try this:
你可以试试这个。它对我有用:
在上面的代码中
createdtime
是数据库字段名称,每个人这个名称可能会有所不同。You can try this one. it worked for me :
In the the above code
createdtime
is database field name, as individuals this name could vary.如果您已经知道日期,那么您可以简单地使用 Between,如下所示:
If you already know the dates then you can simply use between, like this: