【问题标题】:How to skip duplicate value while fetching from My sql DB从 Mysql DB 获取时如何跳过重复值
【发布时间】:2015-10-22 17:06:40
【问题描述】:

我有一个问题。我正在从 MYSQL 表中获取一些数据。但是有一些重复的数据。我需要跳过那些重复的数据。我在下面解释我的代码。

session_start();
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$colg_id=1;
$dept_id = $_SESSION["admin_dept_id"];
$user_id=$_SESSION["admin_id"];
$connect = mysqli_connect("localhost", "root", "******", "go_fasto");
$result = mysqli_query($connect, "select plan_id,unit_name  from db_unit_plan where dept_id='".$dept_id."'  and user_id = '".$user_id."'   ");
while ($row =mysqli_fetch_assoc($result)) {
  $data[] = $row;
}
    print json_encode($data);

这里我需要如果任何 unit_name 列具有相同类型的数据,那么如何跳过这些行。请帮助我。

【问题讨论】:

标签: php mysql


【解决方案1】:

用 DISTINCT 像这样改变

$result = mysqli_query($connect, "select DISTINCT unit_name,plan_id  from db_unit_plan where dept_id='".$dept_id."'  and user_id = '".$user_id."'   ");

【讨论】:

    【解决方案2】:

    您必须指定“DISTINCT”关键字才能从 SQL 中获得唯一的结果。 所以只需尝试将您的选择语句更改为$result = mysqli_query($connect, "select DISTINCT plan_id .. ");

    祝你好运。

    【讨论】:

      猜你喜欢
      • 2016-07-01
      • 1970-01-01
      • 2014-07-26
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      相关资源
      最近更新 更多