mysql sql一个星期前_MYSQL经典SQL之星期问题

几个星期相关的函数

WEEKDAY()

取值为0~6,对应星期一到星期日,每周以星期一为第一天

DAYOFWEEK()

取值为1~7,对应星期日到星期六,每周以星期日为第一天

DAYNAME()

取值为Sunday~Saturday,对应星期日到星期六,第一天是星期日;

该函数与参数lc_time_names有关,如果设置此参数值为”zh_CN”,则得到对应的中文名称“星期日”~“星期六”

示例:

mysql> set @day='2017-01-02';

Query OK, 0 rows affected (0.00 sec)

mysql> select weekday(@day),dayofweek(@day),dayname(@day);

+---------------+-----------------+---------------+

| weekday(@day) | dayofweek(@day) | dayname(@day) |

+---------------+-----------------+---------------+

| 0 | 2 | Monday |

+---------------+-----------------+---------------+

2017-01-02是星期一,使用这三个函数分别得到的是0,2,Monday

修改lc_time_names参数后,得到中文名称:

mysql> set lc_time_names='zh_CN';

Query OK, 0 rows affected (0.00 sec)

mysql> select weekday(@day),dayofweek(@day),dayname(@day);

+---------------+-----------------+---------------+

| weekday(@day) | dayofweek(@day) | dayname(@day) |

+---------------+-----------------+---------------+

| 0 | 2 | 星期一 |

+---------------+-----------------+---------------+

WEEK()

Mode

First day of week

Range

Week 1 is the first week …

0

Sunday

0-53

with a Sunday in this year

1

Monday

0-53

with 4 or more days this year

2

Sunday

1-53

with a Sunday in this year

3

Monday

1-53

with 4 or more days this year

4

Sunday

0-53

with 4 or more days this year

5

Monday

0-53

with a Monday in this year

6

Sunday

1-53

with 4 or more days this year

7

Monday

1-53

with a Monday in this year

该函数用于获取日期是年度中的第几周。该函数比较复杂,使用不同的mode,得到不同的结果。见下表:

Mode

First day of week

Range

Week 1 is the first week …

0

Sunday

0-53

with a Sunday in this year

1

Monday

0-53

with 4 or more days this year

2

Sunday

1-53

with a Sunday in this year

3

Monday

1-53

with 4 or more days this year

4

Sunday

0-53

with 4 or more days this year

5

Monday

0-53

with a Monday in this year

6

Sunday

1-53

with 4 or more days this year

7

Monday

1-53

with a Monday in this year

例如,mode值为1,则每周的第一天为周一,week()函数的结果为0~53,如果第一个周天数少于4,则记为第0周,如果第一个周天数大于等于4,则记为第1周。

再如,mode值为2,则每周第一天为周日,week()函数的结果为1~53,如果第一个周里包含了周日,则记为第1周,如果第一个周里没有周日,则记为上一年度的最后一周。

实际上,这个参数主要是为了解决跨年的周该如何归属的问题,是算作本年度的第一周,还是上一年度的最后一周,又或者是算作第0周。这需要根据使用场景和习惯来选择。

但不管怎么归属,week函数本身的取值范围有限,所以再跨年的时间区间一般不用此函数(后边举例说明)。

按周分组问题

假设我们有一张销售表,内容如下:

mysql> select * from sales;

+----+---------------------+------+

| id | date | cost |

+----+---------------------+------+

| 1 | 2010-12-31 00:00:00 | 100 |

| 2 | 2011-01-01 00:00:00 | 200 |

| 3 | 2011-01-02 00:00:00 | 100 |

| 4 | 2011-01-06 00:00:00 | 100 |

| 5 | 2011-01-10 00:00:00 | 100 |

+----+---------------------+------+

现在,我们要统计每周的销售额。

首先想到的是用week函数,计算日期对应的周数,然后按照这个周数来进行分组求和:

mysql> select week(date), sum(cost) from sales group by week(date);

+------------+-----------+

| week(date) | sum(cost) |

+------------+-----------+

| 0 | 200 |

| 1 | 200 |

| 2 | 100 |

| 52 | 100 |

+------------+-----------+

如果能保证这个日期区间是在一个年度内的,那么用week函数完全没有问题。

很不幸的是,通常日期区间是跨年的,例如我们这个示例中的数据,恰好有跨年的周,2010-12-31日是第52周,2011-01-01变成了2011年度的第0周,而实际上这两天是在同一周。

要解决这个问题,我们不能指望week函数,因为该函数的返回结果总是在0~53循环,我们需要找一个固定时间为第一周,之后的周数累加而非循环。

例如,我们选择2010-01-03为第一周的第一天,之后的任意一天201x-xx-xx距离2010-01-03的天数/7记为周数,得到结果如下:

mysql> select floor(datediff(date, '2010-01-03')/7) as weekcount,sum(cost) from sales group by floor(datediff(date, '2010-01-03')/7);

+-----------+-----------+

| weekcount | sum(cost) |

+-----------+-----------+

| 51 | 300 |

| 52 | 200 |

| 53 | 100 |

+-----------+-----------+

以上解决方案中,我们选择2010-01-03为起始日期,因为它离我们要统计的时间足够远,同时它是星期日(我们认为周日是一周的第一天)。

如果我们需要把星期一当作第一天,只需要改为2010-01-04即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值