我在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
有什么可能获得“当天的照片”?
规则,例如:
Real rating value
最多的照片photo_of_day
表格 UPD1。
10票表示 - 每张照片的表ratings
中至少有10条记录
UPD2。是否有可能获得确切日期时间的“当日照片”?例如,如何获取“2011-03-11”或“2011-01-25”的当天照片?
答案 0 :(得分: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
中。
答案 1 :(得分: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