gpt4 book ai didi

php - SQL Distinct 与 INNER JOIN

转载 作者:行者123 更新时间:2023-11-29 22:54:48 24 4
gpt4 key购买 nike

我的选项有问题如果我在查询中嵌入DISTINCT,我就会得到value= ""。如果我删除 DISTINCT 我有 value= "1" 并且我需要这个。

 $query_result = mysqli_query($db, "SELECT DISTINCT C.name FROM Category AS C INNER JOIN MarketProduct AS MP ON C.Category_ID = MP.ID_Category WHERE MP.ID_Market ='$id'");

<select name="ID_Category">
<?php
while($row = mysqli_fetch_array($query_result)){
?>

<option value="<?php echo $row['ID_Category'] ; ?>"><?php echo $row['name'] ; ?></option>


<?php
}
?>
</select>

最佳答案

使用GROUP BY,而不是DISTINCT。见下文:

  $query_result = mysqli_query($db, "SELECT C.name FROM Category AS C INNER JOIN MarketProduct AS MP ON C.Category_ID = MP.ID_Category WHERE MP.ID_Market ='$id' GROUP BY C.name");

关于php - SQL Distinct 与 INNER JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755531/

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