SPlatten
SPlatten

Reputation: 5762

SQL query with regular expression

I have a field in a table that contains 1,2,3, what I need is a way to check this field for a match with something like:

SELECT
  t1.something
FROM
  tblContent t1
WHERE
  1 IN (SELECT comma_delimited_string FROM tblAnother WHERE match=t1.ref)

Where 1 in the above query is the data I want to match in the 'comma_delimited_string' returned by the sub query.

This doesn't work and only returns a match if 1 is the first item in the subquery.

I've been reading about regular expressions, but have never used regular expressions in SQL.

If the comma_delimited_string contains 11,2,3 and not 1 then a match should not be returned, it should only match if the comma_delimited_string contains 1 or 1, or ,1

Upvotes: 0

Views: 36

Answers (1)

SPlatten
SPlatten

Reputation: 5762

Solved by using:

    SELECT
      t1.something
    FROM
      tblContent t1
    WHERE
      FIND_IN_SET(1, (SELECT comma_delimited_string FROM tblAnother WHERE match=t1.ref))

Upvotes: 1

Related Questions