MySQL流行的行基于不同的时间级别

时间:2013-08-21 08:06:20

标签: mysql

我有2个表:photolike。我试图在一天内根据喜欢的数量获得流行的照片。所以基本上像'今天流行的'

SELECT
  p.id AS id, COUNT(li.id) AS total_likes \
FROM `photo` p \
  LEFT JOIN `like` li \
    ON p.id = li.photo_id \
WHERE
  li.date > DATE_SUB(CURDATE(), INTERVAL 1 DAY) \
GROUP BY \
  p.id

今天有足够数量的喜欢时效果很好。但如果过去一天没有喜欢的话,它将不会返回任何记录。

我也想稍微改变一下。是否有可能达到水平?例如:
根据多天排名照片:

1. Get photos based on how many likes today
2. Get photos based on how many likes for last week
and so on...

所以基本上它的作用是,假设我们需要得到30个项目。首先,它会尝试根据今天有多少喜欢来获取行。它可能是任何数字20,15等。然后它将获得总共30个所需的剩余行,但现在将根据一周内有多少喜欢进行排序。

So something like:
SELECT FROM photo SORT BY likes today, likes in a week ...

感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

您可以根据CASE计算不同的计数,例如过去30天的喜欢与最后一天的计数。上周和过去30天:

SELECT
  p.id AS id
  ,COUNT(CASE WHEN li.DATE > DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN li.id END) AS daily_likes
  ,COUNT(CASE WHEN li.DATE > DATE_SUB(CURDATE(), INTERVAL 7 DAY) THEN li.id END) AS weekly_likes
  ,COUNT(li.id) AS total_likes
FROM `photo` p
JOIN `LIKE` li
    ON p.id = li.photo_id
WHERE
  li.DATE > DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY
  p.id
ORDER BY daily_likes DESC, weekly_likes DESC, total_likes DESC
LIMIT 30

我不知道您的限制基于哪个定义,可能类似于

SELECT *
FROM
 (
   SELECT
     p.id AS id
     ,COUNT(CASE WHEN li.DATE > DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN li.id END) AS daily_likes
     ,COUNT(CASE WHEN li.DATE > DATE_SUB(CURDATE(), INTERVAL 7 DAY) THEN li.id END) AS weekly_likes
     ,COUNT(li.id) AS total_likes
   FROM `photo` p
   JOIN `LIKE` li
       ON p.id = li.photo_id
   WHERE
     li.DATE > DATE_SUB(CURDATE(), INTERVAL 30 DAY)
   GROUP BY
     p.id
  ) AS dt
ORDER BY
   case when daily_likes > 20 then daily_likes else 0 end desc,
   case when weekly_likes > 100 then weekly_likes else 0 end desc,
   total_likes DESC
LIMIT 30

答案 1 :(得分:1)

你试过UNION吗?

technet.microsoft

Techonthenet.com

Wikipedia

而不是EXISTS

MySQL

使用UNION,您可以选择多个select语句,只要列相同

从头顶开始,像是

(select p.id as id,
       count(li.id) as total_likes
  from photo p,
       like li
 where p.id = li.photo_id
   and li.date > date_sub(curdate(), interval 1 day)
   and rownum <= 10
 order by total_likes
 group by p.id)
 union
(select p.id as id,
       count(li.id) as total_likes
  from photo p,
       like li
 where p.id = li.photo_id
   and li.date > date_sub(curdate(), interval 7 day)
   and rownum <= 10
   and not exists (select p.id as id,
                    count(li.id) as total_likes
                     from photo p,
                          like li
                    where p.id = li.photo_id
                      and li.date > date_sub(curdate(), interval 1 day)
                      and rownum <= 10
                    order by total_likes
                    group by p.id)
 order by total_likes
 group by p.id)

没有测试过这个,但是猜测像这样的东西可以解决这个问题

答案 2 :(得分:0)

您可以计算从今天到&#39;喜欢&#39;之间经过的周数。他们的日期和小组。

SELECT
  p.id AS id, COUNT(li.id) AS total_likes, period \
FROM `photo` p \
  LEFT JOIN `like` li \
  ON p.id = li.photo_id \
WHERE
  DATEDIFF(li.date, now())+7)%7 AS period \
GROUP BY \
  p.id, period
ORDER by period, total_likes DESC 
LIMI 30