2012-01-18 12 views
2

私は2つのテーブル - ConversationsParticipantsを持っています。 user_idSQL結合または複数選択?

conversation_idは、今私は2つ(またはそれ以上)の特定のユーザーが参加しているすべての会話を見つけたい -

参加者は、2つの列があります。

は、誰もがいずれかを持っている...

select conversation_id from participants where id = 123 and id = 456 

が、行は同時に2つのuser_idsを持つことができないので、それは明らかに非常にうまく動作しませんでした:

私はこのような何かを試してみましたどのような方法からここに行くのか?複数の選択クエリ?

答えて

0

試してみてください。

SELECT conversation_id FROM participants WHERE user_id = 123 
INTERSECT 
SELECT conversation_id FROM participants WHERE user_id = 456 
+0

このことによって、グループではありませんでした選択リストから列を削除するように編集あなたの投稿を編集することはできますか?私はダウンフォートを削除します;これはうまくいかないかもしれませんが(またはそれはわかりません) –

+0

@JeremyHolovacsこれは私がダウンしたものとは違って見えます。当時のそれをより明確にしたはずです!私の元の答えを投稿した直後、私は "あなたは何を考えていたのですか?"と終わり、私は戻ってきました。もう一度。 –

+0

これは2人の参加者にとって美しく、簡単に機能しますが、3人目の参加者のために交差/選択を繰り返す必要があります。 –

0

どのようにこのようなものについて:

select distinct c.* 
from conversations as c 
inner join participants as p1 
on p1.conversation_id = c.conversation_id 
where 
p1.user_id = 123 
and exists (select null from participants as p2 where p2.conversation_id = c.conversation_id 
    and p2.user_id = 456); 
3
select conversation_id, count(distinct user_id) 
from participants 
where user_id in (u1, u2, u3...) 
group by conversation_id 
having count(distinct user_id) > 1 

更新:あなたのDBMSは、count(明確な...)をサポートしていない、あなたがしている場合各会話に各ユーザーが重複していないことを確認してください。

select conversation_id, count(*) 
from participants 
where user_id in (u1, u2, u3...) 
group by conversation_id 
having count(*) > 1 
+0

-1これは、OPが求めている特定のユーザIDをチェックしません。 –

+0

+1 u1、u2に代入されます。しかし、 'group by'節が必要です。 –

+0

@JeremyHolovacs 'where'は大したことではなかったので、あなたは' group by'を逃しました:) –

3

試してみてください。

select conversation_id 
from participants 
where user_id in (123, 456) 
group by conversation_id 
having count(distinct user_id) > 1 
+0

+1初めて正しく書いたのです:) –

+0

sqlが対話IDだけが表示されていたとしても、質問は会話IDだけでなく会話用でしたために。しかしこれは実行可能な解決策です。 –

+0

はいは完璧に動作します!感謝万円;私は再び家にいるときにこれをもう少し解剖します – toschenolle

0

私の全体の最初の試みをスクラップするように編集。それは間違った答えでした。私の新しい答えは:

declare @usersToFind table (userid int) 
insert into @usersToFind 
       select 11 
    union all select 12 
    union all select 13 

select p.convid, count(*) [participantCount] 
from 
    @participants p 
    left join @conversations c 
     on p.convid = c.id 
where 
    p.userid in (select userid from @usersToFind) 
group by 
    p.convid 
    -- identify all conversation columns you care about here, and repeat them in select clause 
having 
    count(distinct p.userid) >= (select count(*) from @usersToFind) 

問題は、検索する特定のユーザーのリストを受け入れないということでした。これは、あなたが何人を指定しても、すべてのユーザーが上記の会話の参加者であるすべての会話を見つけます。

興味を持っている場合は、ここで私は私のテストを設定するために使用されるSQLは次のとおりです。

declare @conversations table (id int) 
declare @participants table (userid int, convid int) 

insert into @conversations values (100) 
insert into @conversations values (200) 
insert into @conversations values (300) 
insert into @conversations values (400) 

insert into @participants values (11, 100) 
insert into @participants values (12, 100) 
insert into @participants values (13, 100) 

insert into @participants values (11, 200) 
insert into @participants values (12, 200) 

insert into @participants values (11, 300) 
insert into @participants values (13, 300) 

insert into @participants values (12, 400) 
insert into @participants values (13, 400) 

(さらにまた

関連する問題