dougai6464 2018-04-03 15:09
浏览 60
已采纳

如何在获得非重复条目时查询重复条目?

I have a table like this..:

Table

I am trying to figure out how to query only one of the duplicate entries while still getting the non-duplicate entries?

$query = "SELECT id, size, color FROM `".$item."` WHERE size in (SELECT size FROM `".$item."`
                GROUP BY size HAVING COUNT(*)>1)";
$resul = $conn->query ($query);
if($resul->num_rows > 0){
    while($r = $resul->fetch_assoc()){
        echo $r["size"];
    }
}
  • 写回答

2条回答 默认 最新

  • duanpai6581 2018-04-03 15:20
    关注

    SQL DEMO

    This select the row with biggest id for each size:

    SELECT t1.id, t1.size, t1.color 
    FROM yourTable t1
    LEFT JOIN yourTable t2
      ON t1.id < t2.id
     and t1.size = t2.size
    WHERE t2.id IS NULL   // if no one is bigger than you, you are the biggest.
    

    Now you filter the one with more then one row

     AND t1.size ( SELECT size
                   FROM yourTable
                   GROUP BY size
                   HAVING count(*) > 1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?