0

How can I check with SQL if a column contains a comma seperated list / string?

My query looks like this:

$modulare = $this->Database->prepare("
    SELECT id, titel, sorting, kuerzel
    FROM cc_produkte
    WHERE veroeffentlichen = 1
    AND kuerzel = ? // Here commas have to be filtered
    ORDER BY sorting, id ASC
")
->execute($pid);

The "kuerzel" column contains the shortcuts of individual products as a comma-separated string. But there are also products that have only a shortcut. Only one of the entries should be read. Unfortunately I can not do anything with the linked post.

Is there a possibility in SQL for this? For example, how does the explode function of PHP?

Codehan25
  • 2,704
  • 10
  • 47
  • 94
  • 5
    tbh I'd redesign the database **not** to have that type of data in :| – CD001 Feb 06 '18 at 13:44
  • 1
    Storing CSV in a column defeats the purpose of a database. – Jacob H Feb 06 '18 at 13:46
  • 2
    The other comment was just removed, but useful if you really just want to check for comma. If you don't care about the values, use `kuerzel LIKE '%,%'`. No need for exploding then. – Malte Hartwig Feb 06 '18 at 13:52
  • The "kuerzel" column contains the shortcuts of individual products as a comma-separated string. But there are also products that have only a shortcut. Only one of the entries should be read. Unfortunately I can not do anything with the linked post. – Codehan25 Feb 06 '18 at 13:53
  • @MalteHartwig With LIKE %,% it works, thanks! – Codehan25 Feb 06 '18 at 13:56
  • What they are implying is to use the strength of a relational database, and join to another table to get the "kuerzel" data – Scuzzy Feb 06 '18 at 13:58
  • @Scuzzy It is already set up a complex database structure. However, I needed this query for a detached solution. – Codehan25 Feb 06 '18 at 14:09

0 Answers0