如何获得当天的照片?

我在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票 当天的照片是自上传时间起24小时内最多
Real rating value
的照片 当天的新照片不得出现在
photo_of_day
表中 UPD1。 10票表示 - 每张照片至少10张表
ratings
中的记录 UPD2。是否有可能获得确切日期时间的“当日照片”?例如,如何获取“2011-03-11”或“2011-01-25”的当天照片?     
已邀请:
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
中。     
像这样的东西。我不确定'当天的新照片一定不能在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
    

要回复问题请先登录注册