5

我有 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 ...

谢谢你的帮助!

4

3 回答 3

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
于 2013-08-27T09:43:24.360 回答
1

你试过UNION吗?

技术网

Techonthenet.com

维基百科

并且不存在

MySQL

使用 UNION,您可以加入 select 几个 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)

没有测试过这个,但猜想这样的事情会做的伎俩

于 2013-08-21T09:12:56.560 回答
0

您可以计算今天和“喜欢”日期之间经过的周数,并按它们分组。

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
于 2013-08-21T13:25:29.097 回答