我正在尝试创建一个查询来rank
根据同一个表中的值更新表中所有记录的字段。
我设法获得了一个SELECT
计算排名的有效查询,但我很难将其转换为UPDATE
. 这是查询:
SELECT
((views_count + comments_count) * (172800 / elapsed)) AS rank
FROM (
SELECT
p.views_count,
(UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(p.createdAt)) AS elapsed,
(SELECT COUNT(*) FROM `comments` AS c WHERE c.photo_id = p.id) AS comments_count
FROM `photos` AS p
) AS m
如何将其植入UPDATE
查询中,以更新每条记录的rank
值?
谢谢!:)
编辑:
我的数据库结构:
[Photo]
id
user_id
views_count
rank
createdAt
[Comment]
id
photo_id
content