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

关于mysql - 从两列获取数据时如何计数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16870538/

10-11 07:24