I have the tables below
Table GroupChat
GroupChatId int
GroupCode varchar(20)
Name varchar(50)
Table GCMembers
ID int
ParticipantUserID int
GroupChatID int
Table Chats
ID
Sender
Receiver
I run the following query
Select distinct GC.GroupChatID, GC.Name
from Chats chats
inner join GroupChats GC on chats.Receiver = GC.GroupCode
inner join GCMembers GCM on GC.GroupChatID = GCM.GroupChatID
where GCM.ParticipantUserID = 3
This works perfectly but I want to only have distinct GroupChatID
. In other words if GroupChatID
already exists in the result of the query don't bother adding it. Value GCM.ParticipantUserID
comes from another table (foreign key) that doesn't have to do anything with this question so I didn't include that here. Any int
value could work.
How can I achieve that? Thanks for your help in advance.
Sample Data
GroupChat
GroupChatID - GroupCode - Name
1 GC1 Group1
2 GC2 Group2
--
Chats
ID - SENDER - RECIEVER
1 2 GC1
2 3 GC2
3 3 GC1
4 3 GC1
GCMembers
ID - ParticipantUserID - GroupChatID
1 2 1
2 3 1
3 3 2
The output should be
GroupchatID:1 and Name:Group1