0

我想知道如何将这 3 个查询连接在一起,因为我只想要一个 JSON 输出,我认为 INNER JOIN 会这样做。但是不知道这个怎么用。有人可以引导我走上正确的道路吗?

  $json = array();
$following_string = mysqli_real_escape_string($mysqli,$_SESSION['id']);
$call="SELECT * FROM streamdata WHERE streamitem_id < '$lastID' AND streamitem_target=".$following_string." OR streamitem_creator=".$following_string." ORDER BY streamitem_id DESC LIMIT 10";
$chant = mysqli_query($mysqli, $call) or die(mysqli_error($mysqli));

$json['streamdata'] = array();

while ($resultArr = mysqli_fetch_assoc($chant)) {

    $json['streamitem_id'] = $resultArr['streamitem_id'];
    $json['streamitem_content'] = $resultArr['streamitem_content'];
    $json['streamitem_timestamp'] = Agotime($resultArr['streamitem_timestamp']);

 $json['streamdata'] = $json;
}

/***** COMMENTS *****/
$check = "SELECT comment_id, comment_datetime, comment_streamitem, comment_poster, comment_content FROM streamdata_comments WHERE comment_poster=".$following_string." ";
$check1 = mysqli_query($mysqli,$check);
$json['streamdata_comments'] = array();

while ($resultArr = mysqli_fetch_assoc($check1)) {

    $json['comment_id'] = $resultArr['comment_id'];
    $json['comment_content'] = $resultArr['comment_content'];
    $json['comment_poster'] = $resultArr['comment_poster'];
    $json['comment_datetime'] = Agotime($resultArr['comment_datetime']);
    $json['comment_streamitem'] = $resultArr['comment_streamitem'];

$json['streamdata_comments'] = $json;
}

/***** USERS *****/

$check = "SELECT * FROM users WHERE id=".$following_string."";
$check1 = mysqli_query($mysqli,$check);
$json['users'] = array();

while ($resultArr = mysqli_fetch_assoc($check1)) {

    $json['username'] = $resultArr['username'];
    $json['id'] = $resultArr['id'];
    $json['first'] = $resultArr['first'];
    $json['middle'] = $resultArr['middle'];
    $json['last'] = $resultArr['last'];

$json['users'] = $json;
}


echo json_encode($json);
}
?>
4

3 回答 3

2

您正在获取不相关的数据,因此您不能在 SQL 级别使用连接。

但是 JSON 不在乎你喂它什么,或者如何喂它。只需构建适当的 PHP 级数据结构,例如

$data = array();
$data['streamdata'] = array();
... insert data from 'streamdata' query...
$data['streamdata_comments'] = array();
... insert comment data ...
$data['users'] = array();
... insert user data ...

这将为您提供一个包含每个查询数据的 3 路数组。然后,您将整个$data结构传递给 json_encode,然后繁荣 - 您在单个数据结构中获得了 3 个未评级的查询,而无需每次都触及 SQL 连接。

于 2012-09-02T21:48:17.833 回答
0

Some previous answers have suggested that you can't join unrelated tables, but these are clearly not unrelated tables. The streamdata and streamdata_comments tables are quite closely related, and the users table maps user ID values in the other tables to names.

At the SQL level, these can be combined easily:

SELECT d.*, c.*, u.*
  FROM streamdata          AS d
  JOIN streamdata_comments AS c ON d.streamitem_ID = c.comment_streamitem
  JOIN users               AS u ON u.user_id = c.comment_poster
 WHERE c.comment_poster = '$following_string'
   AND d.streamitem_id < '$lastID'
   AND (d.streamitem_target  = '$following_string' OR
        d.streamitem_creator = '$following_string');

Whether the result makes sense for wrapping into a JSON string is a different matter, on which I can't pontificate. This would give you one record from the comments information for each comment associated with each stream item.

于 2012-09-02T22:28:56.220 回答
0

您正在获取不相关的数据。仅当要连接的数据具有关系时,连接数据才有用。

你不能加入苹果、牛和猴子。

于 2012-09-02T21:43:27.290 回答