2013-06-01 45 views
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 
+1

這兩個表是如何鏈接的? (哪個字段?) – hims056

+0

這些表之間的關係是什麼? – sdespont

+0

唯一的同一列是hfm_files'ID' ... – user2301881

回答

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 ... 
1
SELECT COUNT(t1.id) FROM 
downloaded t1, hfm_files t2 
WHERE t2.id = t1.hfm_file_id 
AND t2.user_id = <user_id> 
相關問題