Click here to Skip to main content
15,896,359 members

Selecting rows where user can be both friend and user

ishandeb asked:

Open original thread
Hey Sorry the title is not very clear.I want to design a table to store friendship relationship in my web project
I have a following friends Table

Friend(Table Name)
friend_id - primary key
user_id
user_id_friend
status

User(Table Name)
user_id- primary key
userName
-----------------------------------------------------------------------
status e.g.(0 denotes rejected whereas 1 denotes accepted or 2 denotes unprocessed
It should satisfy at least the following 4 conditions:

The way the table is populated is - when I send a friend request to John - my userID appears in user_id and Johns userID appears in user_id_friend.

Now another scenario is say Mike sends me a friend request - in this case mike's userID will appear in user_id and my userID will appear in user_id_friend

So to find all my friends - I need to run a query to find where my userID appears in both user_id column as well as user_id_friend column

What I am trying to do now is - when I search for user say John - I want all users Johns listed on my site to show up along with the status of whether they are my friend or not and if they are not - then show a Add Friend button.

If any of you are experienced with this , any suggestion is welcomed

my current design (I think bad right now)

please suggest me a sql to retrieve the data.

thanx in advance.
Tags: C#, SQL, ASP.NET, SQL Server, SQL Server 2008

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900