0

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 concat them 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 query:

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?

kunsterD93
  • 101
  • 10
FallenA
  • 15
  • 3
  • Do you want to keep one AskeMeyer etc? – jarlh Feb 23 '22 at 14:05
  • 1
    A [mcvce] is a great start when asking SQL questions. We still don't know if you want to delete all duplicates, or if you want to keep one instance of each. (Which one?) – jarlh Feb 23 '22 at 15:19
  • 1
    Add a unique constraint to your table, to avoid similar mishaps in the future. – jarlh Feb 23 '22 at 15:21

2 Answers2

1

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

1

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
Cargo23
  • 3,064
  • 16
  • 25