Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

Connect friends and user table together

Hello everybody,
I'm running into a problem with developing my site.
People can become friends of one and eachother and I want these people to see their friends a a specific page.

I have two tables:

Table 1: Friends
ID
user_1
state_1
user_2
state_2

Table 2: users
ID
First_Name
Last_Name
etc

People are friends once their ID is in user_1 or user_2 and both states are set to '1'.

Now I need to connect to the database and make a query where it pulls your friends ID and connects this to the users databes to get First_Name, Last_Name, etc.
How do I do this?

One of the problems is that the friends ID can be in column user_1 or user_2, so how do I filter this?

I saw a questions pretty similiar to mine, with a good answer, but just couldn't get this to work!
http://www.experts-exchange.com/Database/MySQL/Q_21498279.html?sfQueryTermInfo=1+friend

Hope you guys can give me some help!
0
RonWon
Asked:
RonWon
  • 3
  • 2
1 Solution
 
dqmqCommented:
I'll propose an answer momentarily, but first I need to question why you have both state_1 and state_2 in the friends table?  That table represents a relationship between two users; it's OK to store the state of the relationship in that table, but it is not so good to store the state of the users in that table.  Can you please clarify what the usage of those columns is?

Now to your question.  I am assuming the "Friends" relationship is bi-directional.  By that I mean participation as user_1 means the same thing as partipating as user_2.  In fact,  a check constraint that user_1 < user_2 would be entirely logical to avoid duplicates or being a friend with yourself.

Anyway, here's the SQL:

Select u.firstname, u.lastname, f.firstname, f.lastname
From
(Select user_1 user, user_2 friend from Friends where status_1=1 and status_2=1
union user_2, user_1 from friends where status_1=1 and status_2=1) r
inner join users u on u.id = r.user
inner join users f on f.id = r.friend
order by u.id









 
0
 
RonWonAuthor Commented:
Hey dgmg,
The different states are used as a check for who asked who as friend!
For instance, when Mike (ID: 005) asks Jen (ID: 009) as friend, the table gives:

ID      user_1    state_1    user_2     state_2
1        005           0             009             2

So, on Jen's page, a query searches for user_2 = 009 and state_2 = 2
When Jen accepts the friend request, a form updates the table to both states 1

ID      user_1    state_1    user_2     state_2
1        005           1             009             1

So this means they have both agreed to the friendship!

I have a session variable made upon login called kt_login_id, this is the main ID of the user.
So when I want to select all records where the user is a friend (so 'kt_login_id' in user_1 or user_2 column).

The query I use to do this is:

SELECT *
FROM Friends
WHERE user_1 = '$_SESSION['kt_login_id']' AND state_1 = '1'  OR user_2 = '$_SESSION['kt_login_id']' AND state_2 = '1'

Now it needs to be expanded so that it pulls the user data from table 'users'. This is the user data  from the column where the ID is not $_SESSION['kt_login_id']!

Hope this all makes sense.
I could get your former code to work, so I hope this explanation helps a bit more!!!!!
0
 
RonWonAuthor Commented:
The last line must say!!!!!!
I !couldn't! get your former code to work, so I hope this explanation helps a bit more!!!!!

Thanks!

0
 
dqmqCommented:
Thanks for the clarification of the state codes.  I would have used a single state code to simplify things, but your way will suffice.  The main disadvantage of your way is that you have to double up your logic (check both status codes to find the "asker").   You could just as well ALWAYS let user_1 represent the asker and eliminate one of the state codes.

You must use a UNION to pick up the "friends" relationship in both directions.

SELECT  Users.FirstName, Users.LastName
FROM Friends inner join Users on Friends.User_2 = Users.Id
WHERE user_1 = '$_SESSION['kt_login_id']' AND state_1 = '1'  
UNION
SELECT Users.FirstName, Users.LastName
FROM Friends inner join Users on Friends.User_1 = Users.Id
WHERE user_2 = '$_SESSION['kt_login_id']' AND state_2 = '1'  






0
 
RonWonAuthor Commented:
This worked!
Thanks a million!
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now