Solved

Connect friends and user table together

Posted on 2008-10-24
5
686 Views
Last Modified: 2010-04-21
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
Comment
Question by:RonWon
  • 3
  • 2
5 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 22798733
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
 

Author Comment

by:RonWon
ID: 22799374
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
 

Author Comment

by:RonWon
ID: 22799402
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
 
LVL 42

Accepted Solution

by:
dqmq earned 250 total points
ID: 22801484
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
 

Author Closing Comment

by:RonWon
ID: 31509746
This worked!
Thanks a million!
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now