Home > Mobile >  How to DELETE in SQL with multiple conditions
How to DELETE in SQL with multiple conditions

Time:02-24

I have a table with one column called name_of, I have a lot of duplicates in this table, I have some python code, that takes all the duplicates and then it concat it into the sql query. But I have tried for some time with no luck.

TableName = users_from_group

Column = name_of

i have tried the following sql queryes:

DELETE FROM users_from_group
WHERE name_of = ('AskeMeyer'), ('testuser'), ('AskeMeyer'), ('testuser'), ('testuser'), ('AskeMeyer')

and I don't understand why this is not working, as this is the format of data for querying adding into the table?

CodePudding user response:

the best way to do this is:

DELETE FROM users_from_group
WHERE name_of IN ('AskeMeyer', 'testuser', 'AskeMeyer', 'testuser', 'testuser', 'AskeMeyer')

If this is useful for you, I would appreciate to mark as resolved. Cheers

CodePudding user response:

As @jarlh mentioned, you probably don't want to delete ALL of the records with these name_of's. I'm guessing you want to keep exactly 1 of each?

The best practice for this scenario is to do your work all in SQL, following a "Delete Duplicate Rows" pattern. There are a couple ways to do this, and it can matter which version of SQL you are using, but this question is a starting point.

Also, just a couple S/O tips:

  1. Include which SQL server you are using in your questions in the future
  2. Include the error message that you receive
  •  Tags:  
  • sql
  • Related