2013-06-21 21 views
0

我有一個包含cookieID,IP,電子郵件和其他數據詳細信息的用戶訪問日誌表。 用戶可能從多個IP訪問過,不同的CookieID,甚至留下不同的電子郵件。 我想檢索特定用戶的所有日誌事件,基於任何共同的標識符,例如:
如何根據SQL中的多個公共標識符檢索行

Row cookieID IP   email 
1: 1  10.0.0.12 [email protected] 
2: 2  10.0.0.12 [email protected] //match to #1 based on common IP 
3: 1  192.168.1.1 null //match to #1 based on common cookieID 
4: 3  192.168.2.2 [email protected] //match to #5 based on common cookieID 
5: 3  10.11.12.13 [email protected] //match to #2 based on email 

我需要找到一個方法來檢索在這種情況下,所有的行,因爲在某些點他們都通過電子郵件或cookieID相關,或通過IP

任何人都有任何想法如何有效地實現?

+0

您可以添加一個樣本結果集嗎? –

回答

0

試試這個:

select t1.row, t2.row 
from table t1 
inner join table t2 
on t1.cookieId = t2.cookieId 
or t1.email = t2.email 
or t1.ip = t2.ip 
where t1.row != t2.row 
0

在許多數據庫中,如果要有效地做到這一點,你要避免在join條件or。單獨加入通常效率更高:

select t1.row, tc.row as cookie_row, te.row as email_row, ti.row as ip_row 
from table t1 left outer join 
    table tc 
    on t1.cookieId = tc.cookieId left outer join 
    table te 
    on t1.email = te.email left outer join 
    table ti 
    on t1.ip = te.ip; 
相關問題