gpt4 book ai didi

php - 如何基于JOIN查询不重复表列?

转载 作者:行者123 更新时间:2023-11-29 09:36:11 25 4
gpt4 key购买 nike

简单地说,这就是我的 index.php 页面的样子:

[comment_id 10]
[commenter_name here]
[reply_id 1]
[replier_name here]

[comment_id 10]
[commenter_name here]
[reply_id 2]
[replier_name here]

[comment_id 10]
[commenter_name here]
[reply_id 3]
[replier_name here]

这就是我需要的样子:

[comment_id 10]
[commenter_name here]
[reply_id 1]
[replier_name here]
[reply_id 2]
[replier_name here]
[reply_id 3]
[replier_name here]

我的代码的作用:单击回复按钮时,对 comment_id 的回复将提交到数据库中的 replies 表中。问题:也许我根据所使用的 JOIN 类型错误地编写了 SELECT 语句,但我的 comment_id 与 replier_names 一起重复回显。结果应该是:每个唯一的 comment_id 以及所有replier_names 都在下面回显。这是我的长代码的虚拟版本:

$sid = $_SESSION['userid'];
$query = $conn->query("
SELECT comments.*, replies.*, users.username
FROM comments
LEFT JOIN replies
ON comments.comid = replies.repcomid
INNER JOIN users
ON comments.comuserid = users.userid
ORDER BY comments.comid DESC;
");
$comreps = [];
while($row = $query->fetch_object()) {
$comreps[] = $row;
}
?>

<?php foreach($comreps as $comrep): ?>
<b><?php echo $comrep->username; ?></b>
<div><?php echo $comrep->repname; ?></div>
<?php endforeach; ?>

@Tim Morton:只是想知道,这就是您所指的方法吗? (见下文)

<?php
$last_username = '';
foreach($comreps as $comrep):
if ($comrep->username != $last_username): ?>
<b><?php echo $comrep->username; ?></b>
<?php endif;
$last_username = $comrep->username; ?>
<div><?php echo $comrep->repname; ?></div>
<?php endforeach; ?>

最佳答案

我能想到三种方法:

  1. 嵌套查询(外循环是评论,内循环是回复)
  2. 根据当前查询构建数据数组
  3. 使用临时变量保存最后显示的评论信息,直到评论信息发生变化

每种技术都有优点和缺点,那么哪种方法最好呢?这取决于...

临时变量

您在编辑中几乎确定了#3。这可能是最有效的方法(一次查询,很少的开销)。唯一的缺点是它在 View 中保留的逻辑比我喜欢的要多(使 View 变得困惑),而且它完全是程序性的,但这是一个品味问题。

数据数组

这也使用单个查询,但使用命名数组来聚合评论。缺点:数组本身可能会让人感到困惑。

首先,如果数据是 json 字符串,您将如何表示数据?

[{
comment:
{
commentor_id:’$comment_id’,
commentor_name:’$commentor_name’
},{
reply
[
{
reply_id:’$reply_id1’,
reply_name:’$reply_name1’
}, {
reply_id:’$reply_id2’,
reply_name:’$reply_name2’
}
]
}
}]

(旁注,当我写这篇文章时,duck 建议了一种更好的方法,我将在此之后展示)

为了使阵列本身进行重复数据删除,我需要进行一项更改:

[{$comment_id:{
comment:
{
commentor_id:’$commentor_id’,
commentor_name:’$commentor_name’
},{
reply
[{$reply_id1:
{
reply_id:’$reply_id1’,
reply_name:’$reply_name1’
}, {$reply_id2:
{
reply_id:’$reply_id2’,
reply_name:’$reply_name2’
}
]
}
}]

在迭代模型中的查询结果时创建此数组:

$data = [];
while($row = $query->fetch_object()) {
$data[$row->comid] = array(
‘comment_id’=>$row->comid,
‘comment_name’=>$row->username,
);
if(!isset($data[$row->comid][‘reply’]) {
$data[$row->comid][‘reply’]=array();
}
$data[$row->comid][‘reply’][$row->repid] = array(
‘reply_id’=>$row->repid,
‘reply_name’=>$row->repname
);

}

然后就可以在 View 中遍历数组结构了:

<?php foreach($data as $comments): ?>
<div><?= $comments[‘comment_name’] ?></div>
<ol>
<?php foreach($comments[‘reply’] as $reply): ?>
<li><?= $reply[‘reply_id’] ?></li>
<?php endforeach; ?>
</ol>
<?php endforeach; ?>

数据数组,第 2 部分

数据库可以使用 aggregate functions 按您需要的方式输出数据。 (我将其留作更多研究)

嵌套查询

这使用模型中的数据访问对象。我没有足够的空间来解释,但请考虑每个 CRUD 对象都能够通过其 id 查找记录

// assuming custom data access objects Comment and Reply each with method ‘find()’
<?php
$comment = new Comment (new DB);
$reply = new Reply(new DB);
?>


<?php $comment->find($comment_id); ?>
<?php while($row = $comment->fetch() ): ?>
<div><?= $row->name?></div>
<ol>
<?php $reply->find($row->id); ?>
<?php while($r = $reply->fetch()): ?>
<li><?= $r->name ?></li>
<?php endwhile; ?>
</ol>
<?php endwhile; ?>

优点:使用可重用的对象并分离模型和 View

缺点:OOP 可以隐藏昂贵的数据库调用,尤其是当它们处于循环中时。但实际上,这不是问题,除非事情变得失控。

关于php - 如何基于JOIN查询不重复表列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57565486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com