-- 验证：查询宠物ID为273/274、玩家拥有≥2条且非最早创建的记录（只查不删）
WITH player_pet_ranked AS (
    SELECT 
        id,  -- 记录主键ID
        player_id,
        (data->>'ID')::INT AS pet_id,  -- 便于核对宠物ID
        "createTime",
        ROW_NUMBER() OVER (
            PARTITION BY player_id 
            ORDER BY "createTime" ASC
        ) AS rn
    FROM "player_pet"
    WHERE (data->>'ID')::INT IN (273, 274)  -- 改为273或274
)
SELECT * 
FROM player_pet_ranked 
WHERE rn > 1  -- rn>1是要删除的记录
AND player_id IN (
    SELECT player_id 
    FROM player_pet_ranked 
    GROUP BY player_id 
    HAVING COUNT(*) >= 2
);



-- 核心删除：保留每个玩家下273/274宠物中createTime最早的那条，删除其余
WITH player_pet_ranked AS (
    SELECT 
        id,  -- 必须取主键ID用于精准删除
        player_id,
        ROW_NUMBER() OVER (
            PARTITION BY player_id 
            ORDER BY "createTime" ASC
        ) AS rn
    FROM "player_pet"
    WHERE (data->>'ID')::INT IN (273, 274)  -- 关键修改：70→273,274
)
DELETE FROM "player_pet"
WHERE id IN (
    SELECT id 
    FROM player_pet_ranked 
    WHERE rn > 1  -- 删除非最早的记录
    AND player_id IN (
        SELECT player_id 
        FROM player_pet_ranked 
        GROUP BY player_id 
        HAVING COUNT(*) >= 2  -- 仅处理拥有≥2条273/274宠物的玩家
    )
);








-- 查询：player_id=136144、宠物ID=303的所有未删除记录
SELECT 
    id,  -- 记录主键ID
    player_id,
    (data->>'ID')::INT AS pet_id,  -- 宠物ID（便于核对）
    "createTime",  -- 创建时间
    deleted_at,    -- 软删除字段（确认是否未删除）
    data           -- 完整的宠物数据（可选，如需查看全部信息）
FROM "player_pet"
WHERE 
    player_id = 136144  -- 精准定位玩家ID=136144
    AND (data->>'ID')::INT = 301  -- 精准定位宠物ID=303
    AND deleted_at IS NULL;  -- 仅查未被软删除的记录（如需包含删除的，可删除此条件）