I'm working on a social web-site project and I need to list "First, Second and Third Degree Contacts" of my contacts. I'm using SQL Server AND C#
Assume a contact
table like this:
For first degree contact:
- If
gulsah
is me then my first degree contacts areburak,sennur
Query I use to select this:
SELECT contact_2 FROM Contacts_Table WHERE contact_1 like 'gulsah'
For second degree contact:
If gulsah
is me again then my second degree contacts are: mali
What makes it difficult is to select contacts of my contacts who are not my first degree contact.
I can select mutual contacts but I guess it is not the right approach.
For example, to select mutual contacts of me (gulsah
) and burak
:
SELECT contact_1 FROM (SELECT * FROM Contact_Test
WHERE contact_2 like 'burak') a
INNER JOIN (SELECT contact_1 FROM Contact_Test
WHERE (contact_2 = 'gulsah')) b
ON a.contact_1 = b.contact_1
This query works but as I said, it's not the right approach for this job.
For third degree contact:
If gulsah
is me again then my third degree contacts are_ mehmet,ahmet
I need to select contacts of my contacts' contacts who are not my first and second degree contact :)
Here is a post from Linkedin which explains contact level.
Thanks for responses.