Record deletion

  • Is there a better way to delete records from one table where not in another table than

    DELETE FROM stagingin

    WHERE id1 + id2 + id3

    NOT IN (select id1 + id2 + id3

    from plans)

  • How about...

    delete S

    from

        stagingin S

        left join plans P on P.id=s.id

    where

        P.id is null


    Cheers,

    Kevin

  • The left join is the preferred method.

  • If you have any indexes on the ID columns, this will probably be fastest:

    DELETE FROM StagingIn

    WHERE NOT EXISTS

    (SELECT *

     FROM Plans

     WHERE ID1 = StagingIn.ID1

      AND ID2 = StagingIn.ID2

      AND ID3 = StagingIn.ID3)



    --Jonathan

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply