php - 如何避免 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
?>
我需要得到它,就像图像上显示的那样,名称只显示一次。
解决方案
如果您使用 mysql,请使用DISTINCT
关键字以获得唯一结果
推荐阅读
- html - 如果存在特定的 CSS 类组合,则更改另一个类的 CSS
- git - 在 git 遥控器之间同步分支的过程
- php - 允许来自我的 ASP.Net MVC 站点的用户访问我的私人 WordPress 站点
- c++ - 用于生成核心文件的 C++ 代码,就像 gdb >> gcore 一样
- javascript - CoreUI 一次只有一个下拉菜单
- asp.net-mvc-5 - @html.dropdown onchange 事件 MVC5
- batch-file - 在 for 循环中使用内置命令失败
- python - 在 Pandas 中查找重复列的重复位置
- vba - UFT 14.00 如何在不同的操作中使用函数的属性
- angular - ionic 3 - 单击 ion-toggle 后显示确认警报