2012-12-13 78 views
0

我需要得到哪些已經取得用戶的ID的不同名單的現場辦理入住手續在比賽,其中也(checkins.ctype =「活」)居住,辦理登機手續三(3)或更多的朋友計數多加入

這裏是DB設計:

enter image description here

我順利取出,其中更有3個,然後用戶現場簽入比賽名單,但我需要看看他們是我的朋友。現在

我的代碼,直到:

 
SELECT DISTINCT 
    f1.id 
FROM 
    fanusers f1 

JOIN checkins c 
ON c.fanuser_id = f1.id 

WHERE 
c.ctype = 'live' 
AND 
c.match_id IN (
           SELECT 
            c1.match_id 
           FROM 
            checkins c1 
           WHERE 
            c1.ctype = 'live' 
           GROUP BY 
            c1.match_id 
           HAVING 
            COUNT(*)> 3 
          ) 

...and he has 3 ore more than 3 friends lived checked-in in the same match (c.match_id) 

任何IDEEA? 感謝

回答

1

什麼關於fanuser_id加入到簽入fanuser_friends,然後再加入到friend_id簽入,由fanuser_id和match_id分組,然後限制上的朋友的數量,結果發現有關朋友的數量?

I.e.

SELECT DISTINCT c.fanuser_id 
FROM checkins c 
INNER JOIN fanuser_friends f on 
f.fanuser_id = c.fanuser_id 
INNER JOIN checkins c2 
on c2.fanuser_id = f.friend_id 
AND c2.match_id = c.match_id 
WHERE c.ctype = 'live' 
AND c2.ctype = 'live' 
GROUP BY c.fanuser_id, c.match_id 
HAVING COUNT(*) >= 3; 
+0

這正是我所需要的。當你有足夠的經驗時,一切似乎都很清楚。謝謝。 – mgm