Brad45
Brad45

Reputation: 199

Question about unions

Okay I'm having a problem. I have a social networking site and have some code that will call statuses from only friends. I want it to call both statuses from friends as well as the user logged in. Kind of the way facebook does.

Tables: members, friends, status

members - Handle

friends - Username, Friend

status - Handle, Type(public or friends)

Here is code that will show the most recent status from a user's friends.

$shawing = "SELECT * FROM status JOIN friends ON status.Handle = friends.Friend where friends.Username='$members[Handle]' and status.Type='friends' ORDER by status.Id DESC" or print mysql_error();

$members[Handle] calls from an include that identifies the user who is logged in.

Someone told me a Union would work, but I've only been able to make one status show up and the code I have now only has one result.

Basically, I want to take the list of usernames from the person's friends list and add the user logged in's username and show only statuses from the friends section with those usernames.

Also, it would be great if I could get a structure for the code because I'm new to the whole JOIN and UNION. I understand it, sure, I just don't understand how it's properly setup.

Upvotes: 0

Views: 70

Answers (1)

Jamie Taniguchi
Jamie Taniguchi

Reputation: 387

The more UNION and JOIN's you use the more overhead you start to accumulate. Once you try to scale this by 10,000 records you'll start seeing this query in your servers slow query log.

I suggest keeping it simple and run two queries.
1st query: Get the users status
2nd query: Get the users friends status'

The first query should be very fast as you're most likely using the users userid (primary key on the table, yes?). Some times the best way is to just keep things simple.

Upvotes: 1

Related Questions