首页 > 解决方案 > 如何避免 MySQL 查询结果中的重复?

问题描述

我有一个 PHP 脚本从 MySQL 查询中获取行,但是,有些serum会显示多次。我宁愿每个serum都只显示一次。

<?php
$typeno = 0;
$categories = [];
$gettestcat = mysqli_query($con,"SELECT * from types where testid = '$id'");

while($gtc = mysqli_fetch_array($gettestcat)){
    $testid = $gtc['testtypeid'];
    $gettesttypename = mysqli_query($con,"SELECT * from tests where id = '$testid'");
    $gtyn = mysqli_fetch_array($gettesttypename);

    if (!in_array($gtyn['sample_type'], $categories)) {
        $categories [] = $gtyn['sample_type'];
    }
}

$countcat = count($categories);
$gettesttypes = mysqli_query($con,"SELECT * from types where testid = '$id'");

while($gtt = mysqli_fetch_array($gettesttypes)){
    $testid = $gtt['testtypeid'];
    $gettesttypenames = mysqli_query($con,"SELECT * from tests where id = '$testid'");
    $gtyns = mysqli_fetch_array($gettesttypenames);

    if ($gtyns['sample_type'] == 'Serum') {
        echo $gi['name'].' - '.$gtyns['test_name'].' - ';
    }
}
?>

<?php  ?>
<?php   
?>

在此处输入图像描述

我需要得到它,就像图像上显示的那样,名称只显示一次。

在此处输入图像描述

标签: phpmysql

解决方案


如果您使用 mysql,请使用DISTINCT关键字以获得唯一结果


推荐阅读