【问题标题】:MySQL query from PHP combining data from two tables来自 PHP 的 MySQL 查询结合来自两个表的数据
【发布时间】:2014-04-13 06:44:09
【问题描述】:

我正在我的页面上开发一个关注系统,我需要只向用户显示他们关注的用户的帖子。我在mysql中有一个名为follow的表,其中包含user1_iduser2_id。如果 id 为 34 的用户正在关注 id 为 45 的用户,则将其存储在该表中。所以我需要的是选择某人关注的所有用户 ID,并从另一个表中找到他们的帖子。 我需要一些命令SELECT * FROM table1 WHERE u_id=[users id-s that they follow]. 我已经尝试过这样的事情:

$id = $_SESSION['user_id'];
$follow_query = mysql_query("SELECT user_two_id FROM follow WHERE user_one_id=$id");
while($follow_array = mysql_fetch_array($follow_query)){

$sql = mysql_query('SELECT home FROM `matches` WHERE `u_id` IN (' . implode(',',array_map('intval', $follow_array)) . ')');
$sql_array = mysql_fetch_array($sql);}

但它不起作用。

这是下表:

显示用户 33 正在关注用户 34 和用户 35。

这是存储我需要显示的数据的表:

但我只需要向用户显示他正在关注的用户的帖子,在这种情况下来自 id 为 34 和 35 的用户。

【问题讨论】:

  • dump implode(',',array_map('intval', $follow_array)) 所以我们知道这些值 - 如果有的话,还有什么错误
  • 不是$follow_query 假吗?

标签: php mysql


【解决方案1】:

您应该使用 JOIN 重新组合您的 2 个查询,以便将处理留给 MySQL。

$id = $_SESSION['user_id'];
$offset = ($pn - 1) * $itemsPerPage;

$query = sprintf("SELECT home, u_id FROM matches m
    JOIN follow f ON m.u_id = f.user_two_id
    WHERE f.user_one_id = '%d'
    AND m.date > DATE_SUB(NOW(), INTERVAL 1 WEEK)
    ORDER BY m_id DESC
    LIMIT %d,%d", $id, $offset, $itemsPerPage);

$result = mysql_query($query);

if (!$result) {
    $message  = 'Invalid query : ' . mysql_error() . "\n";
    $message .= 'Whole query:  : ' . $query;
    die($message);
}

while ($row = mysql_fetch_assoc($result)) {
    echo $row['home'];
    echo $row['u_id'];
}

mysql_free_result($result);

【讨论】:

  • 我能以某种方式添加 WHERE 日期 > DATE_SUB(NOW(), INTERVAL 1 WEEK) order by m_id DESC 吗?
  • 谢谢,我认为这行得通......再问一个问题......我可以包括 $limit = 'LIMIT' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage ; ...我认为这看起来像这样: $query = sprintf("SELECT home, u_id FROM matches m JOIN follow f ON m.u_id = f.user_two_id WHERE f.user_one_id = '%d' AND m.date > DATE_SUB (NOW(), INTERVAL 1 WEEK) ORDER BY m_id DESC" $limit, $id);
  • 我再次编辑,向您展示您可以向 sprintf 函数添加任意数量的参数。另见the PHP documentation for this function
  • 非常感谢你,V!
【解决方案2】:

$follow_array 正如你所命名的那样,是一个数组。您实际上想要访问 $follow_array['user_two_id']

【讨论】:

    【解决方案3】:
    $id = $_SESSION['user_id'];
    #select ID of users who are following a particular user
    $follow_query = mysql_query("SELECT user_two_id FROM follow WHERE user_one_id='{$id}'");
    #create an array in which we will contain "home" of users
    $homes = array();
    while($follower_id = mysql_fetch_array($follow_query)){
        $query = mysql_query("SELECT home FROM matches WHERE u_id='{$follower_id}'");
        while ($home = mysql_fetch_array($follow_query)){
            $homes[] = $home;
        }
    }
    

    变化:

    • 在传递给查询时将 {} 放在 PHP 变量周围。
    • 关注第一个用户的用户的主页现在位于数组主页中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      相关资源
      最近更新 更多