mysql group by week,基于int dateid(yyyymmdd)

时间:2015-03-18 06:42:35

标签: mysql sql

我读过几个问题,例如MySQL grouping by week, based on a date column?

但我的日期类型为int(11)(我无法更改结构)以存储类似 20150101 的内容来代表 2015年1月1日。我应该如何使用WEEK方法根据此进行分组?

像:

select week(SOMETHING_TO(dateid)) as weeknum, game ,count(*) 
from table where game is not null group by weeknum , game order by weeknum desc;

1 个答案:

答案 0 :(得分:1)

我认为您需要先将int(11)值更改为date,然后使用Week函数,即

select WEEK(DATE(dateid)) as weeknum, game ,count(*) 
from table

DEMO

编辑: -

根据您的意见,您可以尝试使用

select concat('last ' ,(WEEK(dateid,1) -WEEK(CURDATE(),1)),' week',' ' )

DEMO

另请详细检查WEEK功能。

  

WEEK()的双参数形式使您能够指定是否   周从周日或周一开始,返回值是否应该是   取值范围为0到53或1到53.如果mode参数为   省略,使用default_week_format系统变量的值。

另见此列表供参考:

M   F.day   Range   Week 1 is the first week …
0   Sunday  0-53    with a Sunday in this year
1   Monday  0-53    with more than 3 days this year
2   Sunday  1-53    with a Sunday in this year
3   Monday  1-53    with more than 3 days this year
4   Sunday  0-53    with more than 3 days this year
5   Monday  0-53    with a Monday in this year
6   Sunday  1-53    with more than 3 days this year
7   Monday  1-53    with a Monday in this year

M = Mode
F.day = First day of week