473,662 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Friend Connection Query

Hey everyone --

I currently have a table friends:

member_id (int 10),
connect_id (int 10),
active (char 1)

which contains friend relationships. When active is set to 1, the
relationship is confirmed. When it is set to 0, by default, it is not
yet confirmed.

For example:

member_id connect_id active
1 2 1 // 1 and 2 are friends
2 3 1 // 1 and 3 are friends
3 1 0 // 3 and 1 are not yet
friends.

I'm looking for an SQL query that would return a member and his
friends 3 levels deep. For example:

1 <-> 2 <-> 3

What would this query look like? What is the most efficient way to
write it? How would it change if it were 4 levels deep?

Now, to make things more complicated, I also have a table, members,
which basically looks like:

member_id (int 10),
username (varchar 20).

How could I also join the members table onto the query so I can get a
detailed result, such as:

ensnare <-> michael <-> tyra

Thanks much for your help! Have a great day.
Jul 17 '05 #1
10 1974
I noticed that Message-ID:
<6e************ **************@ posting.google. com> from ensnare contained
the following:
What would this query look like? What is the most efficient way to
write it? How would it change if it were 4 levels deep?


I don't think you will do it in a single query. I think you will have
to get a list of the members and for each member (in a while loop)get a
list of the friends. Repeat for each level.

You don't need an active column. If they are not friends, don't put an
entry in the table.

The last bit is a simple join.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2
You don't need an active column. If they are not friends, don't put an
entry in the table.

What if the active field is used to temporarily deactivate someone from the
friend list.
You'd need the entry, but do not want them to show.
In that case...active is a valid solution.

Mich
Jul 17 '05 #3
"Geoff Berrow" <bl******@ckdog .co.uk> wrote in message
news:6q******** *************** *********@4ax.c om...
I noticed that Message-ID:
<6e************ **************@ posting.google. com> from ensnare contained
the following:
What would this query look like? What is the most efficient way to
write it? How would it change if it were 4 levels deep?


I don't think you will do it in a single query. I think you will have
to get a list of the members and for each member (in a while loop)get a
list of the friends. Repeat for each level.


Well, you can actually, by left joining the table to itself repeatedly.

SELECT * FROM friends the_dude
LEFT JOIN friends his_friend
ON the_dude.connec t_id = his_friend.memb er_id
LEFT JOIN friends his_friends_fri end
ON his_friend.conn ect_id = his_friends_fri end.member_id
WHERE the_dude.member _id = %d
AND the_duce.active = 1
AND his_friend.acti ve = 1
AND his_friends_fri end.active = 1
Jul 17 '05 #4
I noticed that Message-ID: <zb************ ********@comcas t.com> from
Chung Leong contained the following:
Well, you can actually, by left joining the table to itself repeatedly.

SELECT * FROM friends the_dude
LEFT JOIN friends his_friend
ON the_dude.connec t_id = his_friend.memb er_id
LEFT JOIN friends his_friends_fri end
ON his_friend.conn ect_id = his_friends_fri end.member_id
WHERE the_dude.member _id = %d
AND the_duce.active = 1
AND his_friend.acti ve = 1
AND his_friends_fri end.active = 1


I think I'm going to stay /well/ away from that. :-}
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #5
"michel" <no@spam.please > wrote in message news:<cb******* ***@news.cistro n.nl>...
You don't need an active column. If they are not friends, don't put an
entry in the table.

What if the active field is used to temporarily deactivate someone from the
friend list.
You'd need the entry, but do not want them to show.
In that case...active is a valid solution.

Mich

Yes, active is necessary because if a person adds a friend, the second
person must confirm that indeed persons 1 and 2 are friends. This
sets active equal to 1.

What would this query look like ?
Jul 17 '05 #6
"Chung Leong" <ch***********@ hotmail.com> wrote in message news:<zb******* *************@c omcast.com>...
"Geoff Berrow" <bl******@ckdog .co.uk> wrote in message
news:6q******** *************** *********@4ax.c om...
I noticed that Message-ID:
<6e************ **************@ posting.google. com> from ensnare contained
the following:
What would this query look like? What is the most efficient way to
write it? How would it change if it were 4 levels deep?


I don't think you will do it in a single query. I think you will have
to get a list of the members and for each member (in a while loop)get a
list of the friends. Repeat for each level.


Well, you can actually, by left joining the table to itself repeatedly.

SELECT * FROM friends the_dude
LEFT JOIN friends his_friend
ON the_dude.connec t_id = his_friend.memb er_id
LEFT JOIN friends his_friends_fri end
ON his_friend.conn ect_id = his_friends_fri end.member_id
WHERE the_dude.member _id = %d
AND the_duce.active = 1
AND his_friend.acti ve = 1
AND his_friends_fri end.active = 1

The problem with this is that it only considers when member_id adds
connect_id as a friend. Friendship is bi-directional. A is friends
with B if A adds B as a friend and confirms or B adds A as a friend
and confirms.

How would this affect the query?

Thanks again.
Jul 17 '05 #7
On 29 Jun 2004 20:21:10 -0700, en*****@gmail.c om (ensnare) wrote:
For example:

member_id connect_id active
1 2 1 // 1 and 2 are friends
2 3 1 // 1 and 3 are friends
3 1 0 // 3 and 1 are not yet
friends.

I'm looking for an SQL query that would return a member and his
friends 3 levels deep. For example:

1 <-> 2 <-> 3


How do you want the result, in one row?
What about the equivalent rows 1,3,2 2,1,3 2,3,1 etc.?

Or given a member_id do you want one row per friend or friend-of-friend to x
levels?

--
Andy Hassall <an**@andyh.co. uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #8

"ensnare" <en*****@gmail. com> wrote in message
news:6e******** *************** ***@posting.goo gle.com...
"Chung Leong" <ch***********@ hotmail.com> wrote in message

news:<zb******* *************@c omcast.com>...
"Geoff Berrow" <bl******@ckdog .co.uk> wrote in message
news:6q******** *************** *********@4ax.c om...
I noticed that Message-ID:
<6e************ **************@ posting.google. com> from ensnare contained the following:

>What would this query look like? What is the most efficient way to
>write it? How would it change if it were 4 levels deep?

I don't think you will do it in a single query. I think you will have
to get a list of the members and for each member (in a while loop)get a list of the friends. Repeat for each level.


Well, you can actually, by left joining the table to itself repeatedly.

SELECT * FROM friends the_dude
LEFT JOIN friends his_friend
ON the_dude.connec t_id = his_friend.memb er_id
LEFT JOIN friends his_friends_fri end
ON his_friend.conn ect_id = his_friends_fri end.member_id
WHERE the_dude.member _id = %d
AND the_duce.active = 1
AND his_friend.acti ve = 1
AND his_friends_fri end.active = 1

The problem with this is that it only considers when member_id adds
connect_id as a friend. Friendship is bi-directional. A is friends
with B if A adds B as a friend and confirms or B adds A as a friend
and confirms.


Database joins are uni-directional unfortunately :-) You would need to
either put in two records per friendship, or use three more queries to fetch
the reverse relationship.

Probably makes more sense to run a query at each level:

SELECT * FROM friends the_dude, friends his_friend
WHERE ((the_dude.acti ve = 1 AND the_dude.connec t_id = his_friend.memb er_id)
OR (his_friend.act ive = 1 AND his_friend.conn ect_id = the_dude.member _id))
AND the_dude.member _id IN ( ... )

Jul 17 '05 #9
Andy Hassall <an**@andyh.co. uk> wrote in message news:<vp******* *************** **********@4ax. com>...
On 29 Jun 2004 20:21:10 -0700, en*****@gmail.c om (ensnare) wrote:
For example:

member_id connect_id active
1 2 1 // 1 and 2 are friends
2 3 1 // 1 and 3 are friends
3 1 0 // 3 and 1 are not yet
friends.

I'm looking for an SQL query that would return a member and his
friends 3 levels deep. For example:

1 <-> 2 <-> 3


How do you want the result, in one row?
What about the equivalent rows 1,3,2 2,1,3 2,3,1 etc.?

Or given a member_id do you want one row per friend or friend-of-friend to x
levels?

this is the full current query:

$query = "SELECT m1.username as member_username ,
ss1.symbol as member_symbol,
m2.username as friend1_usernam e,
ss2.symbol as friend1_symbol,
m3.username as friend2_usernam e,
ss3.symbol as friend2_symbol
FROM ".$CONFIG['tbl_members']." m1,
".$CONFIG['tbl_organizati ons']." o1,
".$CONFIG['tbl_s_symbols']." ss1

LEFT JOIN ".$CONFIG['tbl_friends']." f1
ON ( ( m1.member_id =
f1.member_id OR
m1.member_id =
f1.connect_id ) AND
f1.active = 1 )
LEFT JOIN ".$CONFIG['tbl_members']." m2
ON ( ( m2.member_id =
f1.member_id OR
m2.member_id =
f1.connect_id ) AND
m1.member_id !=
m2.member_id )
LEFT JOIN ".$CONFIG['tbl_s_symbols']." ss2
ON m2.symbol_id =
ss2.symbol_id
LEFT JOIN ".$CONFIG['tbl_friends']." f2
ON ( ( m2.member_id =
f2.member_id OR
m2.member_id =
f2.connect_id ) AND
f2.active = 1 )
LEFT JOIN ".$CONFIG['tbl_members']." m3
ON ( ( m3.member_id =
f2.member_id OR
m3.member_id =
f2.connect_id ) AND
m2.member_id !=
m3.member_id )
LEFT JOIN ".$CONFIG['tbl_s_symbols']." ss3
ON m3.symbol_id =
ss3.symbol_id

WHERE m1.random_key = '$random_key'
AND o1.subdomain = '$subdomain'
AND o1.organization _id =
m1.organization _id
AND ss1.symbol_id = m1.symbol_id";
but for some reason it returns duplicates.

the idea is this:

for the specified member (found by random_key), find his friends and
then for each of those friends, find their friends.

and, for each friend, we fetch the username frmo the members table,
the organization from the organizations table, and the symbol from the
symbols table.

but why is this returning duplicates?
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3141
by: HiChetu | last post by:
hi All, Connection Pool - Does not check for Server Status. Here is the scenario : 1) SQL Server is up 2) Create a connection pool for a particular connection string by repeatedly executing the same query in .NET ( using SqlConnection and SqlCommand classes )
6
8564
by: Andre Eisenbach | last post by:
Should this code compile or not? class A { void f(); }; class B { friend void A::f();
6
1322
by: Brian Henry | last post by:
is there a way to tell when the database connection is retrieveing data? i saw the connectionstate.fetching enumerated value, but of course it's not implemented yet... is there another way to do this? thanks!
1
3501
by: Digital Fart | last post by:
hi I want to wrap the access to a sqlite database in an object. So i can create multiple instances of this wrapper with base code to get data out of the sqlite database. But i want to make the connection only onces when i create the first instance. And when the program ends the connection should be removed. ex.
8
9628
by: Greg Strong | last post by:
Hello All, The short questions are 1 Do you know how to make DSN connection close in Access to Oracle 10g Express Edition? &/or 2 Do you know how to make a DSN-less pass-through query work from
1
3580
by: Pradeep83 | last post by:
Hi All Problem : I am unable to retrieve the data from the table in postgres database using C application which i have written in solaris os. Query: How to check whether connection is there between postgres database and C application(which we have written for accessing the database) ? My code is comiling successfully if i run it then its giving o/p as following : output
0
6896
bartonc
by: bartonc | last post by:
This is a work in progress (current and active). There are some issues to resolve in supporting multiple connection types and I plan to add PySQLite to the mix. The this update is due to my suggesting the accompanying helper functions in a recent post and noticing that the ones posted here are a bit out of date. I'll post those helpers in a separate thread. I've also been working with the following database engines: MySQL 5.x SQLite3 JET...
5
1910
by: tuananh87vn | last post by:
hi, I'mm writing script for adding friend to a friend list.i'm using a form including friend_name (text), message (textarea) and below is my code: if(isset($_POST)) { include('dbconnect.php'); $friend=$_POST; $user=$_COOKIE;
5
4999
by: vieraci | last post by:
Hi, I'm wanting to start a database connection at the start of an app and keep it open for all methods and classes that I'm #including in my project. First I went about it by creating the connection in the Main source file outside of any class declarations and then declaring it external in other source files. // Main cpp file #include <mysql++> mysql::Connection * conn;
0
8764
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8546
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.