gpt4 book ai didi

php - 使用 PHP 和 MySQL 从数据库获取数据时无法避免重复数据

转载 作者:行者123 更新时间:2023-11-29 11:51:31 25 4
gpt4 key购买 nike

我有一个问题。使用 PHP 和 MySQL 从数据库检索时,我无法避免重复数据。我在下面解释我的代码。

   <?php
require_once '../../include/dbconfig.php';
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$colg_id=$request->colg_id;
$result = mysqli_query($connect, "SELECT DISTINCT dept_id from db_unit_plan where clg_id='".$colg_id."' ");
if($row =mysqli_fetch_assoc($result)) {
//$dept_id = $row[dept_id];
//$result = mysqli_query($connect, "select * from db_department where colg_id='".$colg_id."' and dept_id='".$dept_id."' ");
$data = array();
while ($row =mysqli_fetch_assoc($result)) {
$data[] = $row;
}

print json_encode($data);
}
?>

db_unit_plan表中,许多行具有相同的dept_id。这里我需要避免重复的数据。请帮助我。

最佳答案

<?php
require_once '../../include/dbconfig.php';
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$colg_id=$request->colg_id;
$result = mysqli_query($connect, "SELECT DISTINCT dept_id FROM db_unit_plan where clg_id='".$colg_id."' ");

$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row['dept_id'];
}

print json_encode($data);

编辑

如果您需要结果中唯一的 db_department 列表:

<?php
require_once '../../include/dbconfig.php';
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$colg_id=$request->colg_id;
$result = mysqli_query($connect, "SELECT DISTINCT db_department.*
FROM db_department
JOIN db_unit_plan ON db_unit_plan.dept_id = db_department.dept_id
WHERE db_unit_plan.clg_id='".$colg_id."' ");

$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}

print json_encode($data);

关于php - 使用 PHP 和 MySQL 从数据库获取数据时无法避免重复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com