tags:

views:

41

answers:

2

I need a table which has two columns:

First column have an ID Second column have an ID to.

like two person who are friend then first's ID in first and second's ID in second but

if I need to check the first user's friend then I need to check by two condition

check the friend by first column 

check the friend by second column

What would be the best way to create this structure.

+3  A: 

You may create table Friends with columns:

   UserId   int
   FriendId  int

And check does user have friends by just one operation:

 SELECT Count(*)
 FROM Friends
 WHERE (UserId = @YourUserId or FriendId = @YourUserId)
Michael Pakhantsov
very very thanks
steven spielberg
A: 

To avoid two checks you should always insert rows with the first ID being greater than the second. That way if you have to check if A and B are friends you will have just one check to do

siukurnin