链接两个表中的信息

时间:2012-10-22 16:43:22

标签: php mysql

我是代表朋友打字的,所以这可能措辞严厉:

我有两张表interestsuser_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))){

这是从interests表中获取信息的代码,但我希望从user_updates

轻松获取相应的数据(正确的更新)

我怎么能这样做?

3 个答案:

答案 0 :(得分:3)

您需要在公共字段上加入两个表:

表名:user_updates和interest

公共字段用户名(公共字段在每个表上必须具有相同的值,以便它们可以相互匹配)

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

答案 1 :(得分:2)

使用连接语句。查看MySQL文档以获取更多信息:http://dev.mysql.com/doc/refman/5.0/en/join.html

答案 2 :(得分:0)

也许这可以帮到你:

<?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++;
}
?>