I need to build a query that will determine if a user is a 'friend' of a friend.
Basically, a query to determine the relationship between two accounts.
The friend's list table is designed as followed
id (int-pk)
member_a
member_b
*member_b is the person who requested the friendship
*member_a is the person who accepted the friendship
I think the quick and dirty way to do it would be to fetch a user's entire friend list, and loop through it seeing if there are any matches. However, this would require potentially thousands of small queries and I'd like to avoid that. I have that working, and the code is below. I'm hoping to find out if there is a way to do this all within a single query.
I think the best way to explain how I need this to work is to just post the code I have that gets the job done. In the example below, I use a situation where two accounts are not 'friends' but they share a friend so they would considered 'friends of friends'.
Basically, a query to determine the relationship between two accounts.
The friend's list table is designed as followed
id (int-pk)
member_a
member_b
*member_b is the person who requested the friendship
*member_a is the person who accepted the friendship
I think the quick and dirty way to do it would be to fetch a user's entire friend list, and loop through it seeing if there are any matches. However, this would require potentially thousands of small queries and I'd like to avoid that. I have that working, and the code is below. I'm hoping to find out if there is a way to do this all within a single query.
I think the best way to explain how I need this to work is to just post the code I have that gets the job done. In the example below, I use a situation where two accounts are not 'friends' but they share a friend so they would considered 'friends of friends'.
<cfset is_friend = 0>
<cfset is_friend_of_friend = 0>
<cfquery name="check_if_friend" datasource="members">
Select list_id
FROM tbl_member_friend_list
Where (member_id_a = #session.myid# and member_id_b = 530) or (member_id_b = #session.myid# and member_id_a = 530)
</cfquery>
<cfif check_if_friend.recordcount gte 1>
<cfset is_friend = 1>
We friends!
</cfif>
<cfif is_friend neq 1>
<cfquery name="get_user_friend_list" datasource="members">
SELECT member_id_b as friend
FROM tbl_member_friend_list
WHERE member_id_a = '530'
UNION ALL
SELECT member_id_a as friend
FROM tbl_member_friend_list
WHERE member_id_b = '530'
</cfquery>
<cfloop query="get_user_friend_list">
<cfquery name="check_if_friend_of_friend" datasource="members">
select list_id
FROM tbl_member_friend_list
where ((member_id_a = #session.myid# and member_id_b = #get_user_friend_list.friend#)
or (member_id_b = #session.myid# and member_id_a = #get_user_friend_list.friend#))
</cfquery>
<cfif check_if_friend_of_friend.recordcount eq 1>
<cfset is_friend_of_friend = 1>
We friends of friends
<cfbreak>
</cfif>
</cfloop>
</cfif>
