mobile
mobile

Reputation: 317

linking information in two tables

I am typing this on behalf of a friend, so this may be worded badly:

I have two tables interests and user_updates:

$feed_query = mysql_query("SELECT `username`, `contents`, (`posted`) AS posted FROM 
    user_updates ORDER BY UNIX_TIMESTAMP(`posted`) DESC");
$num_rows = 0;
while(($num_rows < 12) && ($row = mysql_fetch_array($feed_query))){

This is the code getting the information from the interests table, but I want to easily get the corresponding data (The correct update) from user_updates

how can i go about doing this?

Upvotes: 0

Views: 75

Answers (3)

Joel Hernandez
Joel Hernandez

Reputation: 2213

Maybe this can help you out:

<?php
$feed_query = mysql_query("SELECT upd.username,upd.contents, (upd.posted) AS posted FROM user_updates upd LEFT OUTER JOIN interests int ON int.id=upd.interest_id  ORDER BY UNIX_TIMESTAMP(upd posted) DESC"); 
$num_rows = 0; 
while(($num_rows < 12) && ($row = mysql_fetch_array($feed_query))){

    echo $row['username']."---".$row['contents']."---".$row['posted']."<br>";

    $num_rows++;
}
?>

Upvotes: 0

Ertun&#231;
Ertun&#231;

Reputation: 829

You need to join two tables on a common field :

table names : user_updates and interests

common field username (Common field must have the same values on each table, so that they can be matched against each other)

select * from user_updates u 
     inner join interests i
on i.username = u.username

Upvotes: 3

user428517
user428517

Reputation: 4193

Use a join statement. Check the MySQL documentation for more: http://dev.mysql.com/doc/refman/5.0/en/join.html

Upvotes: 2

Related Questions