473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to run a Inner join on same table?

Hi,

I have a table UserDetails with following fields -
UserID (Primary key for table UserDetails)
UserName - User's Name
Surname - User's Surname.

There is one another table FriendData with following fields -
FID (Primary key for table FriendData)
UserID (UserID from table UserDetails)
FriendID (Friend's ID which is again User id from table UserDetails).


The table UserDetails contains info about users and table FriendData contain information about friends.

For ex. table UserDetails have the following data -
UserID UserName Surname
001 User1 Sur1
002 User2 Sur2
003 User3 Sur3
004 User4 Sur4
005 User5 Sur5
006 User6 Sur6
007 User7 Sur7
008 User8 Sur8
009 User9 Sur9
010 User10 Sur10

and the table FriendData have following data -
FID UserID FriendID
00a 002 009
00b 009 002
00c 002 007
00d 007 002
00d 001 002
00e 002 001
00f 001 007
00g 007 001
00h 003 002
00i 002 003
00j 009 004
00k 004 009

now, friends of user 002 are 009, 007, 001 & 003.
I want friends of friends for any given user.
In this case, for 002 the answer is - 001 & 004.
But as 001 is already friend of 002, so the answer is 004.

Kindly suggest query for this. I am using MSSql.
Dec 2 '10 #1
1 3271
code green
1,726 Expert 1GB
An INNER JOIN ON the same table is no problem.
SImply write two joins giving each a different alias.
I don't understand your structure but
Expand|Select|Wrap|Line Numbers
  1. JOIN tablename AS friend1 JOIN sametablename AS friend2
Then to read fields from either join you need
Expand|Select|Wrap|Line Numbers
  1. SELECT friend1.fieldname AS 1_friend, friend2.fieldname AS 2_friend...
Dec 3 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Steve | last post by:
Hi, I am real new to databases and hoping someone can help. Main-table is a huge spreadsheet that I imported into Access Site-table has a bunch of addresses pulled from Main-table, quite a few...
4
by: algroth | last post by:
Hi! I try to do a nested inner join by sql, but I always get an "Syntax-error in FROM-clause". The statement I try around with is: SELECT * FROM st1 INNER JOIN st2 INNER JOIN st3
0
by: Bonzol | last post by:
hey there, This is for a search function /What I want to do is, return a datatable with all the ID's that have the required values. This is easy enough when everything I want to enter is in the...
6
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: ...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
3
by: Zeff | last post by:
Hi all, I have a relational database, where all info is kept in separate tables and just the id's from those tables are stored in one central table (tblMaster)... I want to perform a query, so...
12
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
2
by: kjewell23 | last post by:
Hello. I'm trying to inner join two tables into one dataset. Is that possible. If so I do I do it. I have my code down below, so if you could help me. please!! I'm using asp.net for my coding Dim...
13
by: rusdyrip | last post by:
i have 2 table table Person ID Name Type 001 A Customer 002 B Sales table Invoice InvoiceID CustomerID SalesID Quantity
2
by: MATTXtwo | last post by:
I have this store procedure to select data from table with join like this...SELECT tblPeribadi.Personel_No, tblPeribadi.Nama,tblCompany.Keterangan as Company_Code, tblPeribadi.Jawatan,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.