共通して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
が必要な場合は、必要な4つの結合をテーブルに追加し、関連する列をSELECT
句に追加できると確信しています。