如何将结果命名为其他名称

时间:2012-07-23 09:07:35

标签: mysql

我在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 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

在mysql站点搜索WEEK()

答案 2 :(得分:0)

使用您在群组对帐单中使用的相同week()功能。

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)

答案 3 :(得分:-2)

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

周功能将返回一年中的周数,我认为这不是您想要的。