Best way to delete large set of rows knowing field to NOT delete
0
votes
2
answers
187
views
Coming from a Rails background, I have a large production database that I have a copy of where I only need records from 4 of the 2000+ companies. I'm trying to delete all the rows except the ones belonging to the 4, and I know the way I have it is not optimal.
DELETE FROM appointments
WHERE (appointments.company_id NOT IN (6, 753, 785, 1611))
another example is when I have to delete records on a table where the company_id is on an associated table:
DELETE FROM mappings
WHERE mappings.id IN (SELECT mappings.id
FROM code_mappings
INNER JOIN codes ON codes.remote_id = mappings.code_remote_id
WHERE (codes.company_id NOT IN (6, 753, 785, 1611)))
Asked by Tonys
(131 rep)
Oct 1, 2016, 03:39 AM
Last activity: Jun 30, 2025, 02:05 PM
Last activity: Jun 30, 2025, 02:05 PM