0

我在 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
4

2 回答 2

2

尝试通过加入两个表来计数:

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 ...
于 2013-06-01T08:11:19.887 回答
1
SELECT COUNT(t1.id) FROM 
downloaded t1, hfm_files t2 
WHERE t2.id = t1.hfm_file_id 
AND t2.user_id = <user_id>
于 2013-06-01T08:12:37.010 回答