PostgreSQL Delete Row where equal to String delimited by comma

Viewed 457

I am beginner to SQL, I have some rows that I wish to delete through the use of executing a query.

For example, I have a table:

     ID                  │           Type            │ 
─────────────────────────┼───────────────────────────┼
 220                     │ aaa                       │ 
 220                     │ bbb                       │
 220                     │ ccc                       │ 
 220                     │ ddd                       │ 
 220                     │ eee                       │ 
 220                     │ fff                       │ 

And I have a string that is delimited by commas:

aaa, bbb, fff, ddd

I wish to delete all rows that are equal to ID of 220 and where Type is equal to a value in the string.

How could I go about this.

Thanks very much

1 Answers

One option splits the string to an array, and then uses any():

delete 
from mytable 
where id = 220 and type = any(string_to_array('aaa, bbb, fff, ddd', ', ' ))
Related