简体   繁体   English

如何从MySQL选择带有子注释的注释?

[英]How to select comments with child comments from MySQL?

I have two tables: 我有两个表:

'comments' '评论'

| id | content | user_id | article_id | parent_id |

'users' “用户”

| id | name | photo |

And my queries are: 我的查询是:

<?php
$query = mysql_query("SELECT comments.id, comments.content, users.name, 
users.photo FROM comments, users WHERE comments.article_id = '".$get_id."' 
AND comments.parent_id = 0 AND comments.user_id = users.id");
while($res = mysql_fetch_assoc($query))
{
  $id = $res['id'];
  $content = $res['content'];
  $name = $res['name'];
  $photo = $res['photo'];

  echo $photo;
  echo $name;
  echo $content;

  $query2 = mysql_query("SELECT comments.id, comments.content, users.name, 
  users.photo FROM comments, users WHERE comments.article_id = '".$get_id."' 
AND comments.parent_id = '".$id."' AND comments.user_id = users.id");
  while($res2 = mysql_fetch_assoc($query2))
  {
    $id2 = $res2['id'];
    $content2 = $res2['content'];
    $name2 = $res2['name'];
    $photo2 = $res2['photo'];

    echo $photo2;
    echo $name2;
    echo $content2;
  }
}
?>

It doesn't work properly. 它不能正常工作。 It shows 1 parent and 1 child comment in each nest although there are several child comments. 尽管有多个子注释,但每个嵌套中显示1个父注释和1个子注释。

How can I fix and minimize it? 如何解决并最小化它? Can it be done by using only one query? 是否可以仅使用一个查询来完成?

Thank you! 谢谢!

JOIN the table on itself. JOIN本身的表。

Change your query to: 将查询更改为:

SELECT comments.id, comments.content, users.name, 
users.photo FROM comments JOIN users ON comments.user_id = users.id JOIN 
comments c ON comments.id = 
c.parent_id  WHERE 
comments.article_id = '".$get_id."' 
AND comments.parent_id = '".$id."'

You don't need the second query, here's the full code: 您不需要第二个查询,这是完整的代码:

<?php

$query = mysql_query("SELECT comments.id, comments.content, users.name, 
users.photo FROM comments JOIN users ON comments.user_id = users.id JOIN 
comments c ON comments.id = 
c.parent_id  WHERE 
comments.article_id = '".$get_id."'");

while($res = mysql_fetch_assoc($query))
{
  $id = $res['id'];
  $content = $res['content'];
  $name = $res['name'];
  $photo = $res['photo'];

  echo $photo;
  echo $name;
  echo $content;
 } 

?>

Just use a JOIN to fetch the comments and with all the children. 只需使用JOIN即可获取注释以及所有子项。

SELECT users.id userID, users.name, users.photo , childrenComments.*,   
   parentComments.id cpId, parentComments.content cpContent,
   parentComments.user_id cpUser_id,parentComments.article_id cpArticleId
FROM users JOIN (SELECT * FROM Comment WHERE id=0) parentComments 
ON users.id=parentComments.user_id LEFT JOIN Comment childrenComments
ON parentComments.id=childrenComments.parent_id;

Then you can the loop through the result to print it appropriately. 然后,您可以遍历结果以适当地打印它。 This would be better as you would have to run just a single query. 这样会更好,因为您只需要运行一个查询即可。

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

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