其他分享
首页 > 其他分享> > 删除多余数据,只保留id最小的数据

删除多余数据,只保留id最小的数据

作者:互联网

删除多余数据,只保留id最小的数据

DELETE
FROM
    ship_plan_vessel_voyage_data
WHERE
    (carrier_code,vessel_name, voyage) IN

    (select carrier_code,vessel_name, voyage from
        (
        SELECT
            carrier_code,
            vessel_name,
            voyage
        FROM
            ship_plan_vessel_voyage_data
        GROUP BY
            carrier_code,
            vessel_name,
            voyage
        HAVING
            count(*) > 1
    )     temp
        )

AND id NOT IN (

    select id from
     (
        SELECT
            min(id) id
        FROM
            ship_plan_vessel_voyage_data
        GROUP BY
            carrier_code,
            vessel_name,
            voyage
        HAVING
            count(*) > 1
    ) temp

)

标签:voyage,code,name,carrier,多余,vessel,数据,id
来源: https://www.cnblogs.com/xudong5273/p/14939149.html