json 如何从两个相关表中收集数据

7fhtutme  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(97)

我尝试从两个不同的表中获取数据:userscomments中的至少一个。我想根据commentsenderid返回注解数据和用户数据。

<?php
$commentpostid = $_POST['commentpostid'];
$sql = "SELECT * FROM comments where commentpostid = '{$commentpostid}'";
$sqll = mysqli_query($db, $sql); // $db->query() is also accepted.
$result = mysqli_fetch_all($sqll, MYSQLI_ASSOC);
echo json_encode($result);

字符串
测试结果:

[
  {
    "commentid": "93",
    "comment": "naber kankam Benin",
    "commentpostid": "1006",
    "commentsenderid": "12"
  },
  {
    "commentid": "95",
    "comment": "kankam selam!",
    "commentpostid": "1006",
    "commentsenderid": "3135"
  }
]


应为:

[
  {
    "commentid": "93",
    "comment": "naber kankam Benin",
    "commentpostid": "1006",
    "commentsenderid": "12",
    "username": "denemeuser",
    "userbolum": "denemebolum",
    "useryas": "useryasdeneme"
  },
  {
    "commentid": "95",
    "comment": "kankam selam!",
    "commentpostid": "1006",
    "commentsenderid": "3135",
    "username": "denemeuser",
    "userbolum": "denemebolum",
    "useryas": "useryasdeneme"
  }
]


[

] [

]

tyg4sfes

tyg4sfes1#

你选择所有的评论,加入他们与用户的id匹配的作者的id的评论,你只选择那些谁的文章匹配的值,你正在寻找。

SELECT c.*, u.username, u.userbolum, u.useryas
FROM comments c
         JOIN users u ON c.commentsenderid = u.userid
WHERE commentpostid = ?

字符串

相关问题