Jump to content

Gathering Data Based on a "friends" status


deth4uall

Recommended Posts

Hi I have these tables roughly that have info in them but I am trying to write a SQL query that will gather them all together in one query... ;)

 

                // User table
	// -------------------------------------
	// userid	username	lastactivity
	// -------------------------------------
	// 1		abc			1244912985
	// 2		def			1244912983
	//
	// Friends table
	// -------------------------------------
	// userid	relationid	friend
	// -------------------------------------
	// 1		2			yes
	// 2		1			yes

I am not versed with the Outer Joins so I would like to see an example of what it would look like to gather info based on your `id` being '1', and both of the above friends table being set to yes. Also I need pretty much all the info in the User table, based on the Friends table (both ids being friends with one another).

Link to comment
Share on other sites

Hi

 

Not exactly sure what you are after. I presume what you want is a list of friends for a particular id. So with your example data you would get 1 as the id and 2 as the friend (and possibly with more data 3, 4, 5, etc also as friends).

 

If so, something like this would do it:-

 

SELECT a.userid, a.username, c.userid, c.username
FROM UserTable a
LEFT OUTER JOIN FriendsTable
ON a.userid = b.userid
AND friend = 'yes'
LEFT OUTER JOIN UserTable c
ON b.relationid = c.userid
WHERE a.userid = 1

 

All the best

 

Keith

 

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.