9lessons programming blog
Loading Search
Thursday, May 13, 2010

Join Tables Relationships with SQL

My previous post Database Design Create Tables and Relationships with SQL. This post is sequel how to join these tables and displaying proper data. I had used these SQL statements at labs.9lessons.info.

database design

Friends
Data relations between users and friends tables. Take a look at the following SQL statement users table object a and friends table object b . Here friend_one = '1' refers to users table user_id value.
SELECT a.username, a.email
FROM users a, friends b
WHERE a.user_id = b.friend_two
AND b.friend_one = '1'
AND b.role = 'fri'
ORDER BY b.friend_id DESC
LIMIT 30 ;
labs.9lessons friends
PHP Code
Contains PHP code. Displaying username srinivas friends results
<?php
$user_id='1'; // User table user_id value
$friends_sql=mysql_query("SELECT a.username, a.email FROM users a, friends b WHERE a.user_id = b.friend_two AND b.friend_one = '$user_id' AND b.role = 'fri' ORDER BY b.friend_id DESC LIMIT 30");
while($friends=mysql_fetch_array($friends_sql))
{
$title=$friends['username'];
$email=$friends['email'];
$lowercase = strtolower($email);
$image = md5($lowercase);
$avatar ='http://www.gravatar.com/avatar.php?gravatar_id='.$image;
?>

<a href="/<?php echo $title; ?>" title="<?php echo $title; ?>"><img src="<?php echo $avatar; ?>" border="0"/></a>

<?php
}
?>

Updates
Data relations between users, updates and friends tables. Take a look at the following SQL statement users table object a , updates table object b and friends table object c . Here friend_one = '1' refers to users table user_id value.
SELECT a.username, a.email, b.update_id, b.update, b.time, b.vote_up, b.vote_down
FROM users a, updates b, friends c
WHERE b.user_id_fk = a.user_id
AND c.friend_one = '1'
AND b.user_id_fk = c.friend_two
ORDER BY b.update_id DESC
LIMIT 15;

PHP Code
Contains PHP code. Displaying username srinivas homeline updates
<?php
$user_id='1'; // User table user_id value
$update_sql=mysql_query("SELECT a.username, a.email, b.update_id, b.update, b.time, b.vote_up, b.vote_down FROM users a, updates b, friends c WHERE b.user_id_fk = a.user_id AND c.friend_one = '$user_id' AND b.user_id_fk = c.friend_two ORDER BY b.update_id DESC LIMIT 15");
while($row=mysql_fetch_array($update_sql))
{
$username=$row['username'];
$email=$row['email'];
$update_id=$row['update_id'];
$update=$row['update'];
$time=$row['time'];
$up=$row['vote_up'];
$down=$row['vote_down'];

//Avatar
$lowercase = strtolower($email);
$image = md5($lowercase);
$avatar ='http://www.gravatar.com/avatar.php?gravatar_id='.$image;

//Update HTML tags filter
$htmldata = array ("<", ">");
$htmlreplace = array ("&lt;","&gt;");
$final_update = str_replace($htmldata, $htmlreplace, $update);


// Updates Results Display here

}
?>

Comments
Data relations between users and comments tables. Take a look at the following SQL statement users table object a and comments table object b . Here update_id_fk = '2' refers to updates table update_id value.
SELECT a.username, a.email, b.comment_id, b.comment, b.time
FROM users a, comments b
WHERE b.user_id_fk = a.user_id
AND b.update_id_fk = '2'
ORDER BY b.comment_id;

Hope you like this. Thanks!

Share this post

Comments
{ 20 comments }
Mitendra Chaurasia said...

Looks nice idea. thanks for sharing.

Adrian said...

Gracias, por publicar articulos de calidad!! eres el amo

bestmoneyplace said...

very best information.give me more on this ...

Anonymous said...

thank you for fulfilling my request! i really appreciate it

AnisDesignz said...

Thank you so much.
Does you will post the voting part?

AnisDesignz said...

Please Help!
How can I add time and user_id in my SQL Table?
Please reply I need this!

Sasi said...

you can add time using the datatype datetime and user_id using either an int or tinyint datatype.

Indian Stock Market Live Statistics said...

this is working fine for friends updates but if we want personal updates also mingled with friends updates to display how do it like twitter [ my+friends updates at a time what is query ]

Fakhri Nurullah said...

Extraordinary master..!!

Anonymous said...

Is it really important to have different tables (freinds table, username table)instead of one ?

hoponhiggo said...

How do you add new friends? Is it a button with some sql behind it?

Anonymous said...

yea..How do you add new friends? Is it a button with some sql behind it??

Anonymous said...

I've noticed your select statement on Friends assumes that friend_one is the user in question, so if 2 people are friends with each other, then there will be 2 entries into Friends?

friend_one = 6, friend_two = 10
friend_one = 10, friend_two = 6

what if your database grows enormously? you will have duplicate data.

Srinivas Tamada said...

Following and Follower concept. If you have better solution for this please suggest.

Matias said...

No sabes lo que me sirvio este tuto!!! mil gracias y felicitaciones ... tenes ejemplos muy copados

Anonymous said...

hello srinivas. i got some problem

my home feed sql query

SELECT M.msg_id, M.uid_fk, M.message, M.created, U.username FROM messages M, user U, friends F WHERE M.owner=U.uid and F.uid_fk = '$uid' and M.uid_fk = F.friend_id_fk order by M.msg_id desc

but it dont show home feed owner(owner of session) posts. friends posts are ok.

what can be wrong?

Home said...

Hi..some one,, can u help me??
how to display SUGGEST FRIENDS with php and MYsql statement??
for the FRIENDS TABLE , you can see at this link http://www.9lessons.info/2010/04/database-design-create-tables-and.htm

Mano said...

Hi Srinivas, how to get all the votes for each status update?

Mano said...

Hi Srinivas, how to get all the votes for each status update?

Thurrock Design said...

As always a great post, friends and relationships between database tables can get confusing...

Thank You

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine
After developing a site you’ll require a web hosting plan to host your site, a recommended host is justhost.com which operates a LAMP environment and has been established a number of years.

Like Me