MySQL每周报告,包含给定的开始日期和结束日期

时间:2012-04-10 10:42:53

标签: mysql group-by

我有一个存储以下数据的表:

recordDate  itemID  price
2012-03-30  2       15.1
2012-03-30  13      15.1
2012-04-06  2       15.2
2012-04-06  13      15.3
2012-04-15  2       16.2
2012-04-16  13      16.3
2012-04-26  2       14.9
2012-04-26  13      15.0
...

我希望每周一次的报告从星期五开始到下一个星期四结束。 此外,所选数据仅在给定日期记录。

例如:startDate = "2012-04-01", endDate = "2012-04-30"

我的预期结果是这样的:

weekStart   weekEnd     itemID   price
2012-04-06  2012-04-12  2        15.2
2012-04-06  2012-04-12  13       15.3
2012-04-13  2012-04-19  2        16.2
2012-04-13  2012-04-19  13       16.3
2012-04-20  2012-04-27  2        14.9
2012-04-20  2012-04-27  13       15.0
...

我怎样才能得到这个结果?我不熟悉SQL所以我无法弄清楚如何编写语句。

2 个答案:

答案 0 :(得分:0)

SELECT 
  DAYNAME('2012-04-06') AS `start_week`,
  DAYNAME('2012-04-12') AS `End_week`,
  item_id,
  price 
FROM
  store 
WHERE record_date BETWEEN '2012-04-06' 
  AND '2012-04-12' ;

试试这个?

答案 1 :(得分:0)

SELECT * from store where DAYNAME(recordDate) in ('Friday','Saturday','Sunday','Monday','Tuesday') and 
start_date = '2012-01-01' and end_date = '2012-04-10'