在以下查询中包含来自周数的日期

时间:2016-02-12 15:18:45

标签: mysql sql

我有以下SQL查询返回回答最多问题的前五周。这个星期是由数字返回的,但我想要一周的第一个日期而不是数字。

SELECT Count(*) AS answered, 
   Week(score_datetime) AS week, 
   Year(score_datetime) AS year 
FROM   development.score 
WHERE  score_datetime IS NOT NULL 
GROUP  BY Week(score_datetime) 
ORDER  BY answered DESC 
LIMIT  5;

我的sql技能并不是最好的,所以我目前通过momentjs返回星期数。因此,如果有人知道如何在上述查询中包含日期,那么周数和年份将会非常受欢迎。

2 个答案:

答案 0 :(得分:1)

您可以在以下网址找到具体问题的答案:How to convert number of week into date?

因此,根据答案,转换公式为:-lzsrtp-x86_64-unknown-linux-gnu -lstdc++

把它放在一起:

WEEKDAY(DATE_ADD(MAKEDATE(year, 1), INTERVAL Week(score_datetime) WEEK))

答案 1 :(得分:1)

尝试这样的事情:

SELECT Count(*) AS answered, 
   Week(score_datetime) AS week,
   DATE_ADD(score_datetime, INTERVAL(1-DAYOFWEEK(score_datetime)) DAY) as week_start,
   Year(score_datetime) AS year 
FROM   development.score 
WHERE  score_datetime IS NOT NULL 
GROUP  BY Week(score_datetime) 
ORDER  BY answered DESC 
LIMIT  5;