我在 SQL 中有 2 个表。
hfm_files => id, user_id, etc...
downloaded => id, hfm_file_id, etc...
我知道我的user id
. 如何使用简单的 SQL 计算一个文件的下载量?我试图进行一些查询,但我无法做到:
UPDATE user u
SET u.affilite_pont = (SELECT COUNT(*) FROM downloaded vm WHERE
尝试通过加入两个表来计数:
SELECT COUNT(d.ID)
FROM downloaded d
JOIN hfm_files h
ON d.hfm_file_id = h.id
WHERE h.user_id = 1;
所以你更新查询应该是:
UPDATE user u
SET u.affilite_pont = (
SELECT COUNT(d.ID)
FROM downloaded d
JOIN hfm_files h
ON d.hfm_file_id = h.id
WHERE h.user_id = 1;
)
WHERE ...
SELECT COUNT(t1.id) FROM
downloaded t1, hfm_files t2
WHERE t2.id = t1.hfm_file_id
AND t2.user_id = <user_id>