2012-09-08 170 views
1

說我有看起來像三個表:相互共享關係獲取計數

users: 
id 

event_registration: 
id 
user_id 
event_id 

events: 
id 

用戶登記的事件並將該登記記錄在event_registration。

有沒有一種方法可以計算一組給定用戶在單個MySQL語句中相互共享的事件計數?我想出瞭如何爲兩個用戶做到這一點,但我想重構 它支持X個用戶(包括只有一個用戶)。

回答

1

嘗試

select count(event_id) as events 
from event_registration 
where user_id in (1,2,3) 
group by event_id 
having count(distinct user_id) = 3 

你必須調整數3having條款acourding到user_id是你正在使用的號碼與您的in條款。