2

我在 MYSQL DB 中有这样的查询

SELECT date_format( doj, '%d/%m/%Y' ) AS week ,COUNT(user_id) FROm user_tb 
WHERE doj between '2012-01-01' AND '2012-01-31' group BY WEEK(doj)

Result

week    COUNT(user_id)
06/01/2012  3
17/01/2012  2
25/01/2012  3
29/01/2012  1

我怎么能这样命名

week    COUNT(user_id)
week1   3
week2   2
week3   3
week4   1
4

4 回答 4

3

如果您想从 1 开始对周数进行编号,请执行以下操作:

SELECT concat('week', @row := @row + 1) AS week, COUNT(user_id)
from user_tb, (select @row := 0) r
WHERE doj between '2012-01-01' AND '2012-01-31' 
group BY WEEK(doj)

编辑

如果您想要一年中的周数,请执行以下操作:

SELECT concat('week', week(doj)) AS week, COUNT(user_id)
from user_tb, (select @row := 0) r
WHERE doj between '2012-01-01' AND '2012-01-31' 
group BY WEEK(doj)
于 2012-07-23T09:11:37.027 回答
1

搜索WEEK()at mysql 站点。

于 2012-07-23T09:13:45.197 回答
0

week()使用您在 group 语句中使用的相同功能。

SELECT CONCAT("week", WEEK(doj)) AS week, COUNT(user_id) 
FROM user_tb 
WHERE doj between '2012-01-01' AND '2012-01-31' 
GROUP BY WEEK(doj)
于 2012-07-23T09:17:48.097 回答
-2

我认为最有效的方法是使用rownum(行号)并将其与“周”连接(不要忘记按周排序,以便行号与周号匹配)。

week 函数将返回一年中的星期数,我认为这不是您要寻找的。

于 2012-07-23T09:13:57.030 回答