mysql中每月的星期功能 [英] Function for week of the month in mysql

查看:87
本文介绍了mysql中每月的星期功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



最好的我是一个简单的函数来获取本月的一周(而不是一年中最容易的一周)。 (dateField) - WEEK(DATE_SUB(dateField,INTERVAL DAYOFMONTH(dateField)-1 DAY)可以提供:

  WEEK )+ 1 

我很想知道我是否在这里重新发明轮子,如果有是一个更简单和更清洁的解决方案?

解决方案

AFAIK,这个月的第一周没有标准。 b
$ b

每年的第一周是包含 1月4日的一周。



您是否定义了每个月的第一周?



更新: 您会(dateField,5) -
WEEK(DATE_SUB(dateField,5) -
WEEK(DATE_SUB(dateField,5))


pre $ SELECT WEEK INTERVAL DAYOFMONTH(dateField) - 1天),5)+ 1

d正确,并且周开始在周一



否则,您的查询就可以了。


I was looking for a simple function to get the week of the month (rather than the easy week of the year) in a mysql query.

The best I could come up with was:

WEEK(dateField) - WEEK(DATE_SUB(dateField, INTERVAL DAYOFMONTH(dateField)-1 DAY)) + 1

I'd love to know if I'm reinventing the wheel here, and if there is an easier and cleaner solution?

解决方案

AFAIK, there is no standard on the first week of month.

First week of year is the week containing Jan 4th.

How do you define first week of month?

UPDATE:

You'll need to rewrite your query like this:

SELECT  WEEK(dateField, 5) -
        WEEK(DATE_SUB(dateField, INTERVAL DAYOFMONTH(dateField) - 1 DAY), 5) + 1

so that the year transitions are handled correctly, and the weeks start on Monday.

Otherwise, your query is fine.

这篇关于mysql中每月的星期功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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