MySQL:如何选择本周的记录? [英] MySQL: How to select records for this week?

查看:123
本文介绍了MySQL:如何选择本周的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天。



我有 temp sqlfiddle.com/#!2/cf1aarel =noreferrer> sqlfiddle

  id(int 11 primary key)
name(varchar 100)
name2(varchar 100)
date(datetime)

我想在本周获得记录,例如,如果现在21.11.2013我想要18.11.2013到24.11.2013(一周)的所有行



现在我看到下一个算法:

  1)获得工作日
2)计算多少天前是星期一
3)计算日期星期一
4)计算未来日期星期天
5)提出请求日期

请告诉我,是否存在较短的算法(最好在查询MySQL中)?



ADD问题:为什么 它查询 选择记录17.11.2013(星期日) - 23.11.2013(星期六) )以及如何获取日期的记录18.11.2013(星期一) - 24.11.2013(星期日)?



查询:

  select * from temp 
where yearweek(`date`)= yearweek(curdate())

谢谢!

解决方案

使用 YEARWEEK()


$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 1)


Good day.

I have table temp with structure on sqlfiddle:

id(int 11 primary key)
name(varchar 100)
name2(varchar 100)
date(datetime)

I would like get record on this week, example if now 21.11.2013 i would like all rows on 18.11.2013 to 24.11.2013(on week)

Now i see next algorithm:

1) obtain weekday
2) calculate how many days ago was Monday
3) calculate the date Monday
4) calculate future date Sunday
5) make a request on date

Tell me please, is exist a shorter algorithm (preferably in the query MySQL)?

ADD Question: why it query select record on date 17.11.2013(Sunday) - 23.11.2013(Saturday) and how get records on date 18.11.2013(Monday) - 24.11.2013(Sunday) ?

query:

select * from temp
where yearweek(`date`) = yearweek(curdate())

Thanks!

解决方案

Use YEARWEEK():

SELECT *
FROM   your_table
WHERE  YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)

这篇关于MySQL:如何选择本周的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆