你說共有3個以上的利息ID,所以你的意思是「至少4」,對不對?
SELECT first1.userid, second1.userid
FROM users_interests first1, users_interests second1,
users_interests first2, users_interests second2,
users_interests first3, users_interests second3,
users_interests first4, users_interests second4
WHERE
first2.userid=first1.userid AND first3.userid=first1.userid AND first4.userid=first1.userid AND
second2.userid=second1.userid AND second3.userid=second1.userid AND second4.userid=second1.userid AND
first1.userid<>second1.userid AND
first1.interestid=second1.interestid AND
first2.interestid=second2.interestid AND first2.interestid<>first1.interestid AND
first3.interestid=second3.interestid AND first3.interestid<>first2.interestid AND first3.interestid<>first1.interestid AND
first4.interestid=second4.interestid AND first4.interestid<>first3.interestid AND first4.interestid<>first2.interestid AND first4.interestid<>first1.interestid
因爲我沒有測試過這個,請記住它可能有錯誤,所以只有在你理解的時候才使用它。
如果您需要相同的其他數量的共同興趣,我相信您可以編寫代碼來動態生成任何數量的查詢。另外,如果您需要名稱,我相信您可以將必要的四個連接添加到interests
表中,並將相關列添加到SELECT
子句中。
驚人!謝謝你,這個作品非常漂亮! – Ryan 2010-08-15 07:21:09