0

我代表朋友输入此内容,因此措辞可能很糟糕:

我有两张桌子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

我该怎么做呢?

4

3 回答 3

3

您需要在一个公共字段上连接两个表:

表名:user_updates 和interests

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

select * from user_updates u 
     inner join interests i
on i.username = u.username
于 2012-10-22T16:55:44.083 回答
2

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

于 2012-10-22T16:45:19.797 回答
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++;
}
?>
于 2012-10-22T16:47:59.933 回答