Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Question about multiple foreign keys, same table

Author  Topic 

blabus
Starting Member

1 Post

Posted - 2011-11-17 : 18:49:17
I'm building an application that allows users to recommend music to each other, and am having trouble building a query that would return a 'stream' of recommendations that involve any of the user's friends. So in other words, the recommendation would be returned if either the sender or recipient is a 'friend' of the current user. This is my database structure:

Recommendations
ID     Sender     Recipient    [other columns...]
-- ------ --------- ------------------
r1 u1 u3 ...
r2 u3 u2 ...
r3 u4 u3 ...


Users
ID      Email      First Name   Last Name     [other columns...]
--- ----- ---------- --------- ------------------
u1 ... ... ... ...
u2 ... ... ... ...
u3 ... ... ... ...
u4 ... ... ... ...


Relationships
ID      Sender     Recipient     Status       [other columns...]
--- ------ --------- -------- ------------------
rl1 u1 u2 accepted ...
rl2 u3 u1 accepted ...
rl3 u1 u4 accepted ...
rl4 u3 u2 accepted ...


So for user 'u4' (who is friends with 'u1'), I want to query for a 'stream' of recommendations relevant to u4. This stream would include all recommendations in which either the sender or recipient is u1 (the friend).

This is my query so far:

SELECT recommendations.*, user1.firstname AS 'senderFirstName', user1.lastname AS 'senderLastName', user2.firstname AS 'recipientFirstName', user2.lastname AS 'recipientLastName' FROM recommendations, users
INNER JOIN users user1 ON user1.id=recommendations.sender
INNER JOIN users user2 ON user2.id=recommendations.recipient
WHERE users.id=recommendations.sender AND (recommendations.sender IN (SELECT sender FROM relationships WHERE recipient='2' AND status='accepted' UNION
SELECT recipient FROM relationships WHERE sender='2' AND status='accepted') OR recommendations.recipient IN (SELECT sender FROM relationships WHERE recipient='2' AND status='accepted' UNION
SELECT recipient FROM relationships WHERE sender='2' AND status='accepted'))


Now, without the INNER JOINs, this query works correctly, returning all of the records from the 'recommendation' table in which either the sender or recipient is a friend of user 'u4' (specified in the Relationships table). However, the INNER JOINs are causing an error. I'm trying to use them because I also need to get the sender and recipient users' first and last names (stored in the 'Users' table), where the recommendations table just has foreign keys for the sender/recipient.

Problem is, I can't figure out how to get that additional info for each of the two users, and especially how to differentiate between the two foreign keys referencing the same table (as shown in the current query, I want to end up with 4 columns for the first and last name of both the sender and recipient, in addition to all the other recommendation table columns).

I'm a SQL novice, so any help is appreciated. Thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-18 : 03:45:17
[code]
SELECT r.*,u1.firstname,u1.lastname,u.firstname,u.lastname
FROM Recommendations r
INNER JOIN Relationships rl
ON ((rl.Sender = r.Sender AND rl.recipient='u4')
OR (rl.Recipient = r.Sender AND rl.sender='u4'))
AND rl.status='accepted'
INNER JOIN Users u
ON u.ID = rl.Recipient
INNER JOIN Users u1
ON u1.ID = rl.Sender
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -