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 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败