简体   繁体   English

如何合并这些循环的MySQL查询?

[英]How can I combine these looped MySQL queries?

I've recently began a comments/replies section for an app. 我最近开始对某个应用程序发表评论/回复部分。
My initial approach has been to query the database for the comments linked to a particular item as below:- 我最初的方法是查询数据库,以获取链接到特定项目的评论,如下所示:

$commentquery = "SELECT projects_comments.*, users.user_url, users.display_name
                   FROM ".$wpdb->prefix."projects_comments projects_comments
              LEFT JOIN ".$wpdb->prefix."users users on users.ID=projects_comments.userid
                  WHERE projectid = '$projectid'
               ORDER BY projects_comments.commentid DESC";  

I then began a foreach loop with the following query inside in order to retrieve the replies: 然后,我使用以下查询开始了一个foreach循环,以获取答复:

if($comments) {
  foreach ( $comments as $c ) { 
    $replyquery = "SELECT project_replies.*, users.user_url, users.display_name
                     FROM ".$wpdb->prefix."project_replies project_replies
                LEFT JOIN ".$wpdb->prefix."users users on users.ID=project_replies.uid
                    WHERE project_replies.cid = '$c->commentid' 
                 ORDER BY project_replies.id DESC"; 
  }
}

In practice this is all working fine, however it strikes me that this is terribly inefficient and that there must be a way to retrieve all the data in one query. 在实践中,这一切都很好,但是令我惊讶的是,这效率极低,并且必须有一种方法可以在一个查询中检索所有数据。 My concern is that with 100 comments on a page I will be performing 100 queries for the comments alone. 我担心的是,在页面上有100条评论时,我将仅对评论执行100条查询。

What is the best method to tackle this and how would I go about combining these queries to produce one large object/array with which to pull the data from. 解决此问题的最佳方法是什么,我将如何组合这些查询以生成一个大对象/数组以从中提取数据。

Or alternatively, how can I at least prevent the 'query-within-the-loop'. 或者,如何至少防止“环内查询”。

What I tend to do to avoid this setup is this: 我倾向于避免这种设置的方法是:

SELECT ...
WHERE `keyfield` IN (".implode(",",$keyvalues).")
SELECT c.*, cu.user_url AS cu_url, cu.display_name AS cu_name,
       r.*, ru.user_url AS ru_url, ru.display_name AS ru_name
  FROM projects_comments AS c
  LEFT JOIN users        AS cu ON cu.ID = c.userid
  JOIN project_replies   AS r  ON r.cid = c.commentid 
  LEFT JOIN users        AS ru ON ru.ID = r.uid
 WHERE c.projectid = $projectid
 ORDER BY c.commentid DESC, r.id DESC

The only detail to worry about is the JOIN instead of possibly using LEFT JOIN when connecting Project_Replies and Project_Comments. 连接Project_Replies和Project_Comments时,唯一需要担心的细节是JOIN而不是可能使用LEFT JOIN。 This will only pick up comments that have at least one reply; 这只会挑选至少有一个回复的评论; using a LEFT JOIN, you'll get comments that have no replies too. 使用LEFT JOIN,您还会收到没有回复的评论。

You can JOIN them like you do the users table 您可以像执行users表一样JOIN他们

    $commentquery = "SELECT projects_comments.*, project_replies.*, users.user_url, users.display_name, users2.user_url as comment_user_url, users2.display_name as comment_display_name
                     from ".$wpdb->prefix."projects_comments projects_comments
                LEFT JOIN ".$wpdb->prefix."project_replies project_replies ON project_replies.cid = projects_comments.comment_id
                LEFT JOIN ".$wpdb->prefix."users users on users.ID=project_replies.uid
                left join ".$wpdb->prefix."users users2 on users2.ID=projects_comments.userid
                    WHERE projectid = '$projectid'  
                 ORDER BY projects_comments.commentid desc, project_replies.id DESC";   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM