2

我在 MySQL 数据库中有两个表:

相片:

PID - PhotoID [PK], 
DateOf - DateTime of uploading, 
UID -UserID (owner of the photo)

评分:

WhoUID - UserID who rated, 
DateOf - DateTime of rate, 
RatingValue - +1 or -1 (positive or negative), 
RatingStrength - coefficient (different for each user who vote)
PID - PhotoID, [FK]

实际评级值 =RatingValue * RatingStrength

获得“今日照片”的可能性有哪些?

规则,例如:

  • 当天的照片必须在现场至少 24 小时(自上传时间起)
  • 当天的照片必须至少有 10 票
  • 当天的照片是Real rating value自上传时间起 24 小时内最多的照片
  • 当天的新照片不得已在photo_of_day表格中

UPD1。ratings10 票意味着 -每张照片至少有 10 条记录

UPD2。是否有可能获得确切日期时间的“当天照片”?例如,如何获取“2011-03-11”或“2011-01-25”的当天照片?

4

2 回答 2

0
select p.PID from photos p, ratings r where
r.PID = p.PID and                             ; Link ratings to photo
p.DateOf >= '$day' and p.DateOf <= '$day' and ; Get the photos uploaded on the specified date
datediff(p.DateOf, now()) > 1 and             ; photo of the day must be on site at least 24 hours
count(r.*) >= 10 and                          ; photo should have at least 10 ratings
not exists(select p.PID from photo_of_day)    ; photo should not appear in photo_of_day
group by p.PID                                ; group the results by PID
order by sum(r.RatingValue*r.RatingStrength) desc ; order descending by RealRating
limit 1                                       ; limit the results to only one

这个查询可能需要一段时间,所以不要在每个页面请求上都这样做是有意义的。您photo_of_day通过在午夜运行一次的脚本将结果存储一次。

于 2011-04-06T07:28:50.967 回答
0

像这样的东西。我不确定“当天的新照片一定不能在 photo_of_day 表中”,但试试这个 -

SET @exact_datetime = NOW();

SELECT p.*, SUM(r.RatingValue * r.RatingStrength) AS real_rating FROM photos p
  JOIN ratings r
    ON p.PhotoID = r.PhotoID
WHERE
  p.DateOf <= @exact_datetime - INTERVAL 1 DAY
GROUP BY
  p.PhotoID
HAVING
  count(*) >= 10
ORDER BY
  real_rating DESC
LIMIT 1
于 2011-04-06T07:41:49.217 回答