1

MySQL query String contains

Hello, I am trying to make an mysql query that looks for a column value that contains a string from a master string I set. So if my master string is '1234567', I would like it to return any results with column values that have '1','2', etc... The above link was as close as to what I can find but I need it comparing in the opposite direction.

eg.:

WHERE '%{$needle}%' LIKE `column`
1
  • think you'll need to split it character by character if you want to look for 1, 2 etc Commented Oct 29, 2013 at 22:05

2 Answers 2

1

You are a little vague about where the various values are being stored (your text uses terms like "master string" but the sample code uses different names).

You can do what you want using regexp. Here is an example:

select 'abcd6efg' regexp concat('.*[', '1234567', '].*')

To be honest, regex is different from like in one important respect. regex returns true if any part of the left string matches, whereas for like the entire string has to match. So, the following also works:

select 'abcd6efg' regexp concat('[', '1234567', ']')

I like to put the explicit wildcards in to avoid mistakes when switching between the two.

You can look up the documentation for regular expressions here.

Sign up to request clarification or add additional context in comments.

Comments

0

How about

WHERE ? LIKE CONCAT('%', `column`, '%')

where ? is a placeholder having a bound parameter with your master value (1234567) because you are using the PDO or MySQLi extension, right?

You're most certainly not using the deprecated MySQL extension, surely.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.