mysql 查询出重复数据的第一条

WITH RankedGoods AS (  
    SELECT  
        a.*,  
        ROW_NUMBER() OVER (  
            PARTITION BY a.name, a.model_size, COALESCE(a.supplier, '')  
            ORDER BY a.created_time ASC  
        ) AS rn  
    FROM  
        pms_assets_goods_type a  
    WHERE  
        a.deleted = 0  
        AND EXISTS (  
            SELECT 1  
            FROM pms_assets_goods_type b  
            WHERE a.name = b.name  
              AND ((a.supplier = b.supplier) OR (a.supplier IS NULL AND b.supplier IS NULL))  
              AND a.model_size = b.model_size  
              AND a.id <> b.id  -- 确保不是与自己比较  
        )  
)  
SELECT *  
FROM RankedGoods  
WHERE rn = 1;

 

posted @ 2024-07-11 09:59  官萧何  阅读(30)  评论(0编辑  收藏  举报