0

I have a database db with a column named type which is a string.

I want to do an SQL query like this one :

SELECT * FROM db WHERE explode(",", type) = 'action'

Yet it doesn't work because I can't use the explode function in an SQL query. So how can I solve this problem ?

  • What is it you're trying to do, check if 'action' is in the comma separated string? – Nathan_Sav Sep 07 '22 at 17:38
  • Is something like https://stackoverflow.com/questions/5033047/mysql-query-finding-values-in-a-comma-separated-string what you are after? – Nigel Ren Sep 07 '22 at 17:38
  • 1
    Any answer will very much depend on your specific RDBMS which you should have [TAGGED](https://meta.stackoverflow.com/questions/388759/why-should-i-tag-my-rdbms) – Stu Sep 07 '22 at 17:52
  • 1
    Now I guess you know the answer to [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) – RiggsFolly Sep 07 '22 at 18:00

1 Answers1

0

Knowing which db you use can be helpful.

If your database have an explode equivalent with that query you don't match since the explode return a "list" so a list can't match a single value.

Anyway, you don't need to explode the column, just match in it.

Assuming that the column is a string like "value_1,value_2,value_3,value_4..."

You have 3 possible match to do:

  • [1] this will match when the column contains only one value

  • [2] this match when value is in first and following position

  • [3] this match when the value is in last position

     SELECT * FROM db WHERE
         "action"=type OR -- [1]
         "action,%" like type OR -- [2]
         "%,action" like type -- [3]
    
Roberto Braga
  • 569
  • 3
  • 8