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

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

问题描述

我在 sqlfiddle 上有带有结构的表 temp:

I have table temp with structure on sqlfiddle:

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

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

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. 获取工作日
  2. 计算几天前是星期一
  3. 计算星期一的日期
  4. 计算未来的星期日
  5. 按日期提出请求

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

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

ADD 问题是: 为什么要这个查询 选择日期 17.11.2013(Sunday) - 23.11.2013(Saturday) 以及如何获取日期 18.11.2013(M​​onday) - 24.11.2013(Sunday) 的记录) ?

ADD Question is: Why this 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) ?

查询:

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

谢谢!

推荐答案

使用 YEARWEEK():

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

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

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