如何将显示限制为不同的记录?

How can I limit the display to distinct records?

如下图,有多余的记录。如何去除冗余?

这是我的代码:

<?php
$YearNow=Date('Y');
include('../connection/connect.php');
$result = $db->prepare("SELECT * FROM studentvotes,student where student.idno = studentvotes.idno");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
    ?>
        <tr class="record" style="text-align:center;">
        <td><?php echo $row['idno']; ?></td>
        <td><?php echo $row['candid']; ?></td>
        </tr>
    <?php
}
?>

只需将 groupby 添加到您的查询中,例如

$result = $db->prepare("SELECT * FROM 
                        studentvotes,student 
                        where student.idno = studentvotes.idno 
                        GROUP BY student.idno
                      ");

更多详情请阅读@pupil Answer。

我希望这对你有用。

您需要使用 GROUP BY.

将您的 SQL 更改为:

SELECT * FROM studentvotes,student where student.idno = studentvotes.idno 
GROUP BY student.idno

使用 GROUP BY,您指定要分组的列(甚至是多列)。

结果按这些列分组,这意味着这些列(如果是多个列的组合)在结果集中只会出现一次。

例如,您的 table 有多个条目 iDno

当我们触发 SELECT 查询时,它将 return 具有多个 iDno.

实例的所有行

如果我们应用 GROUP BY,它将 return 结果按 iDno 分组。

可以使用GROUP BY不显示多余的记录。

$result = $db->prepare("SELECT * 
             FROM   studentvotes, 
             student 
             WHERE  student.idno = studentvotes.idno 
             GROUP  BY student.idno
");