1

Problem description: I have a table which primary key is id and I also have an array of ids. The array contains ids from the table but some ids of the table may be missing in the array. I need to delete the rows of which ids are missing in the array.

My approach: I fetch all the ids from the table and sort the two array. Then comparing two array I find out the missing ids and deleted corresponding rows.

Is there any better( easy and/or efficient) way to do this?

Shafi
  • 1,850
  • 3
  • 22
  • 44
  • have you tried using IN clause? I think it might be faster. http://stackoverflow.com/questions/7418849/in-clause-and-placeholders – Gennadii Saprykin Jun 20 '16 at 23:13
  • @GennadiiSaprykin I didn't yet. Thanks for suggestion. I need to delete the missing ones. How IN can help me? – Shafi Jun 20 '16 at 23:25

1 Answers1

1

You can write a delete statement like the following:

DELETE FROM table_name WHERE id NOT IN (1, 2, 3, ...)

Karakuri
  • 38,365
  • 12
  • 84
  • 104