我有两张桌子,我将它们连接在一起
<?php
$userqry="
SELECT *
FROM permission_category c
JOIN permission_group g
ON c.perm_group_id = g.id
";
$stmt = $conn2->prepare($userqry);
$stmt->execute();
while($row= $stmt->fetch()){
} ?>
<tr>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['perm_group_name'];?>
</td>
<td>
</tr>
<?php } ?>
但是我正在寻找的结果应该显示如下
我怎样才能实现这个目标?
此代码“记住”循环中以前的名称。如果名称不更改,则不会显示:
<?php
$userqry = "SELECT * FROM `permission_category`
INNER JOIN `permission_group` ON
`permission_category`.`perm_group_id`=`permission_group`.`id`";
$stmt = $conn2->prepare($userqry);
$stmt->execute();
$current_name="";
while ($row= $stmt->fetch()) {
if ($row['name'] != $current_name) {
$name = $row['name'];
$current_name = $name;
} else {
$name = "";
}
?>
<tr>
<td><?php echo $name;?></td>
<td><?php echo $row['perm_group_name'];?>
</td>
<td>
</tr>
<?php } ?>
注意:您应该在查询中添加
ORDER BY
以便以正确的方式对结果进行排序(也许它已经排序)