【问题标题】:How can I combine 2 seperate MySQL queries in a foreach statement如何在 foreach 语句中组合 2 个单独的 MySQL 查询
【发布时间】:2014-05-21 05:40:18
【问题描述】:

假设我有一些帖子,这些帖子包含 cmets,有些帖子有超过 1 条评论,而另一些只有 1 条评论。

我必须从该帖子中获取用户的所有数据以及从对该帖子发表评论的人那里获取用户信息。现在说我必须回显所有这些数据。我会先用一个foreach 和另一个foreach 在里面有点像这样!

所以我做了 2 个 MySQL 查询 $postinfo 包含帖子的所有用户信息,第二个 MySQL 查询 $cmets 包含每个帖子的所有 cmets。

现在这可行,但我想知道是否有更好的方法,更好的做法?我想解决这个问题的原因是因为我想对 cme​​ts 进行 AJAX,以便它们可以自动更新,这种方式似乎很慢

<?php
foreach ($postinfo as $info) {
    echo "<div id='container'>
              <div id='userpost'>
                  <p>" . $info['firstn'] . "</p>
                  <p>" . $info['posttext'] . "</p>
              </div>"; 
    foreach ($comments as $comment) {
        echo "<div id='comments'>
                  <p>' . $comment['firstn'] . '</p>
                  <p>' . $comment['commenttext'] . '</p>
              </div>"
    }
   echo "</div>";
}
?>

此查询在 foreach 中运行以获取特定帖子的评论。

$comments = regular_query(
    "SELECT a.from_who, a.dateposted, a.topostid, a.commenttext, b.firstn
    FROM postcomments a 
    INNER JOIN users b ON a.from_who = b.id
    WHERE a.topostid = :postid",
    ["postid" => $post_idr], $conn);

这个在foreach之外:

$postinfo = regular_query(
    "SELECT b.id, b.from_user, b.dateadded, b.posttype, b.posttext, b.photoname, d.firstn, d.lastn, e.status 
    FROM board b 
    INNER JOIN userprofiles c 
    INNER JOIN users d 
    INNER JOIN friendship e 
    ON b.from_user = c.user_id 
    AND b.from_user = d.id 
    AND e.friend_ids = b.from_user 
    WHERE e.status = 'Friend' 
    AND e.user_ids = :id
    ORDER BY b.id DESC",
    ["id" => (int)$user_id], $conn);

【问题讨论】:

  • 发布一些示例输出
  • 这是缓慢的原因,是因为您(可能)执行查询以分别获取每个帖子的 cmets。您可以通过在一个查询中获取所有相关帖子的 cmets 来加快速度。查询该信息后,也许您可​​以将postid 设为多维 cmets 数组的键。但这只是一个猜测。代码 sn-p 不清楚执行什么查询以及何时执行,也没有查询,没有表结构和索引的描述,也没有对数据量的估计。 “似乎迟缓”也应该是一个具体的衡量标准。
  • 是否可以使用 mysql 抓取与 1 行有关的多行?如果我获取 40 多行 @GolezTrol
  • 请显示将数据导入 $userinfo 和 $cmets 的 SQL
  • @Mikpa 我更新了它

标签: php mysql


【解决方案1】:

流程一: 您可以使用一个查询来获取一篇文章及其所有评论。但它有一个问题,例如一篇帖子有 20 个 cmets,因此它将获得 20 次帖子数据 查询:

Select post.* form post  join comment on post.postId=comment.postId where post.postId='your specific post id'

[您可能需要稍微更改查询,否则可能有一些错字,因为我没有在表格上检查它]

过程二: 有两个变量一个是 dic 另一个是数组 发布数据将在字典中,其中包含一个元素,其中包含评论数组

       $postData={
       postId:1,
       comment:({commentId:1,comment:"bla  bla bla"},
                {commentId:2,comment:"bla  bla bla"}),
       post:"bla bla post"
       }

对于帖子,您必须运行

 Select * from post where postId=your specific id

并发表评论

Select * from comment where postId=your specific id

就这么简单

编辑:

for(int i=0;i<postResult.count;i++)
{
  if(i==0)
 {
    Print Your Post Info//so your post info will be echoed once
 }
    Print Your comment info//all your comment will be echoed here if any


}

【讨论】:

  • 这是mysql,进程二是什么意思?
  • 对于进程1或2?
  • 检查我编辑的答案,但你必须用 php 编写它,我刚刚做了一个演示流程。如果有任何混淆,请告诉我
  • 据我了解,这 for 将在 foreach 右侧,但是说 postid1 有 3 个 cmets 而 postid2 有 19 个 cmets 我将如何使用您的演示代码?
  • 是的,对于每个帖子条目,您必须这样做
【解决方案2】:
Select postId,postTitle,postDesc,postedBy,postedAt,
commentId,commentTitle,commentDesc,commentedBy,commentedAt 
from postTable JOIN commentTable ON postTable.postId = commentTable.postId 
WHERE postTable.postId = 1 ORDER BY commentTable.postId DESC;

像这样迭代数据:

{posts:[
        {postId:1,postTitle:"title",postDesc:"desc",comments:[{comment1},{comment2}]},
        {postId:2,postTitle:"title",postDesc:"desc",comments:[{comment1},{comment2}]}
        {postId:3,postTitle:"title",postDesc:"desc",comments:[{comment1},{comment2}]}
    ]
}

使用 foreach 迭代数据

foreach($posts as $post){
    foreach($comments as $comment){

    }
}

【讨论】:

  • 如果我要使用这个。我会用外部 foreach 来回显布局,而内部 foreach 也将包含一个回声,你无法用另一个回声来回显一个 foreach。你明白吗?
  • 是的,您必须在两个 foreach 中都回显布局...如果您觉得困难...使用一些模板引擎...从后端返回 json 对象...并附加 json定义了模板引擎的对象...因此您无需迭代或回显布局..
  • 不,我不想使用 jquery 或 ajax 但只有 php mysql 来回显这个。
  • 然后在 php 变量中定义整个布局并替换您想要的值,然后回显它。例如:$html = "
    POST_TITLEPOST_DESC
    "; $html = str_replace("POST_TITLE",$post['postTitle'],$html) ; $html = str_replace("POST_DESC",$post['postDesc'],$html) ;回声 $html;
猜你喜欢
  • 2013-07-31
  • 1970-01-01
  • 2011-06-06
  • 2018-07-16
  • 2015-02-21
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
相关资源
最近更新 更多