如何在某天之前和之后的几天内获得mysql的平均评分?

时间:2013-11-15 06:45:52

标签: mysql select group-by

我需要获得视频的平均评分,因为这个视频有一个名为发布日期的字段,所以我需要在发布日期之前和发布日期之后获得评级,

现在我的查询只能获得视频发布日期之前的评分,如下所示

select d.mth Month,
  coalesce(avg(t.rating), 0) Rating
from 
(
  select 1 mth union all
  select 2 mth union all
  select 3 mth union all
  select 4 mth union all
  select 5 mth union all
  select 6 mth union all
  select 7 mth union all
  select 8 mth union all
  select 9 mth union all
  select 10 mth union all
  select 11 mth union all
  select 12 mth 
) d
left join wp_fatalcut_videos_rating t
  on d.mth = month(t.ratingdate)
where d.mth in (1,2,3,4,5,6,7,8,9,10,11,12) and videoid='3192763' and ratingdate <=  '2013-10-09 00:00:00'
group by d.mth

我需要获得同一查询中“2013-10-09 00:00:00”之后的后视频发布的评级,这是可能的吗?

1 个答案:

答案 0 :(得分:1)

你能做到这一点吗?如果我理解你的错误:

select d.mth Month,
  coalesce(avg(t.rating), 0) Rating,
  coalesce(avg(tafter.rating), 0) RatingAfter
from 
(
  select 1 mth union all
  select 2 mth union all
  select 3 mth union all
  select 4 mth union all
  select 5 mth union all
  select 6 mth union all
  select 7 mth union all
  select 8 mth union all
  select 9 mth union all
  select 10 mth union all
  select 11 mth union all
  select 12 mth 
) d
left join wp_fatalcut_videos_rating t
  on d.mth = month(t.ratingdate) 
  AND t.videoid='3192763' and t.ratingdate <=  '2013-10-09 00:00:00'
left join wp_fatalcut_videos_rating tafter
  on d.mth = month(tafter.ratingdate) 
  AND tafter.videoid='3192763' and tafter.ratingdate >=  '2013-10-09 00:00:00'
where d.mth in (1,2,3,4,5,6,7,8,9,10,11,12)
group by d.mth

我认为这是陈述是不必要的

where d.mth in (1,2,3,4,5,6,7,8,9,10,11,12)

因为你不会在表mth中有月份,例如0,13或14.右边?