1

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?

6 Answers 6

3
DELETE FROM your_table
WHERE id NOT IN (1,2,3)
Sign up to request clarification or add additional context in comments.

Comments

2
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 ...)

Comments

1
delete from your_table where id not in (1, 2, 4)

Comments

1

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.

Comments

1

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)

Comments

1

You should try this query:

DELETE FROM tablename WHERE ID NOT IN ('1','2','3');

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.