Jump to content

Problem with joins


sciencebear

Recommended Posts

I had originally posted this over in the PHP help board, but it turned into a problem that could be better answered over here. This is the query I'm currently running:

 

SELECT t1.username,t1.type,t1.path,t3.body,t4.id FROM updates t1
      LEFT JOIN friends t2 ON t1.username=t2.username
      LEFT JOIN posts t3 ON t1.username=t3.username
      LEFT JOIN userpics t4 ON t1.path=t4.path
      WHERE t2.friendname = '$username' OR t2.username = '$username'
      ORDER BY t1.id DESC

 

$username is defined. I'm trying to find updates from friends of the current users. But there is a problem.

 

Imagine user1 is friend with user2, user3 and user4. They have made updates like this:

 

user2 update 1

user3 update 2

user4 update 3

user2 update 4

user2 update 5

user3 update 6

 

Instead of returning that, the query returns something like:

 

user2 update 1

user2 update 4

user2 update 5

user3 update 2

user3 update 6

user4 update 3

user2 update 1

user2 update 4

user2 update 5

user2 update 1

user2 update 4

user2 update 5

user3 update 2

user3 update 6

 

Any suggestions?

Link to comment
Share on other sites

Hi

 

Does your friends table have a single row or 2 rows for each friendship?

 

Ie, if Bill and John are friends would you have:-

 

friends

username, friendname

Bill, John

John, Bill

 

If so something like this would do it:-

 

SELECT t2.username,t2.type,t2.path,t3.body,t4.id 
FROM friends t1
LEFT JOIN updates t2 ON t1.friendname=t2.username
LEFT JOIN posts t3 ON t2.username=t3.username
LEFT JOIN userpics t4 ON t2.path=t4.path
WHERE t1.username = '$username'

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

You could maybe use distinct, but you shouldn't need to. It would be better to find the underlying issue rather than paper over it.

 

Try something like this:-

 

SELECT t2.username,t2.type,t2.path,t3.body,t4.id 
FROM (SELECT username AS friendname FROM friends WHERE friendname = '$username' UNION SELECT friendname AS friendname FROM friends WHERE username = '$username' ) t1
LEFT JOIN updates t2 ON t1.friendname=t2.username
LEFT JOIN posts t3 ON t2.username=t3.username
LEFT JOIN userpics t4 ON t2.path=t4.path

 

This is doing a subselect to get a list of all the users friends and then joining that result with the other tables.

 

All the best

 

Keith

Link to comment
Share on other sites

The current query I am running is:

 

SELECT t2.username,t2.type,t2.path,t3.body,t4.id

FROM (SELECT username AS friendname FROM friends WHERE friendname = '$username' UNION SELECT friendname AS friendname FROM friends WHERE username = '$username') t1

LEFT JOIN updates t2 ON t1.friendname=t2.username

LEFT JOIN posts t3 ON t2.date=t3.stamp

LEFT JOIN userpics t4 ON t2.date=t4.date

ORDER BY t2.id DESC

 

This works but I'd like a better solution.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.