Reputation: 317
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
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
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
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