Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I am having some trouble with a MySQL query. I have a field in a table called "ID", I want a query that deletes all rows in that table unless they have a certain ID, I want more to be able to define more than 1 ID in the query.
Any ideas?
DELETE FROM your_table WHERE id NOT IN (1,2,3)
Add a comment
DELETE FROM yourTable WHERE yourID NOT IN (1,2,3,4) -- place your list here
Or of you do not want to list your ids, you can use a subquery which would contain the list of ids you want to keep:
DELETE FROM yourTable WHERE yourID NOT IN (SELECT * FROM yourTable WHERE ...)
delete from your_table where id not in (1, 2, 4)
This should do the job
delete from TABLE_NAME where id not in (1,2,3,4,5,6)
of (1,2,3,4,5...) is a list of id's you want to save.
You can also use this with some extra conditions:
delete from your_table_name where id not in (select id from your_table_name where necessary=false)
You should try this query:
DELETE FROM tablename WHERE ID NOT IN ('1','2','3');
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.