如何获取当天的照片?

发布于 2024-10-30 19:40:31 字数 823 浏览 1 评论 0原文

我在 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 小时内,
  • 当天的新照片不得已存在于 photo_of_day

UPD1 中。 10 票意味着 - 每张照片 UPD2 的评分表中至少有 10 条记录

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

I have two tables in MySQL database:

photos:

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

ratings:

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]

Real rating value = RatingValue * RatingStrength

What are possibilities to get "Photo of the day"?

Rules, for example:

  • photo of the day must be on site at least 24 hours (since uploaded time)
  • photo of the day must have at least 10 votes
  • photo of the day is the photo with maximum Real rating value in 24 hours since uploaded time
  • new photo of the day must not be already in photo_of_day table

UPD1.
10 votes means - at least 10 records in table ratings for each photo

UPD2. Is it possible to get 'photo of the day' for exact datetime? For example, how can I get photo of the day for '2011-03-11', or '2011-01-25' ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风夜微凉 2024-11-06 19:40:31
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 中。

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

This query probably takes a while, so it makes sense to not do this on every page request. You store the result in photo_of_day once by a script that runs once at midnight.

空城旧梦 2024-11-06 19:40:31

像这样的东西。我不确定“当天的新照片不得已在 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

Something like this. I'm not sure about 'new photo of the day must not be already in photo_of_day table', but try this one -

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文