0

I'm wanting to check if in my column named "tag" it contains numbers with two digit numbers using Regex.

my query:

$FilterTag="3,2,11"
$sql = "SELECT * FROM table1 WHERE bTown = ? AND REGEXP_LIKE(tag, '.*[$FilterTag]')";

My table: db table tag

the only problem is that my query is finding single character numbers ,like instead of telling me that it contains 11 it will tell me that it contains 1.

Is there any way that i can group a character set of numbers?

4
  • Are 3, 2 and 11 specifically the only numbers you're trying to match? Commented Feb 7, 2016 at 17:22
  • i'm wanting to match numbers from 1 to 99 Commented Feb 7, 2016 at 17:23
  • The regex for that would be the one Enissay posted. Removed mine because it matches 0-99; I missed that you didn't want 0 included. Commented Feb 7, 2016 at 17:25
  • What DB are you using? Can you show what data your currently have and what you should and shouldn't get? Maybe make a sqlfiddle? Commented Feb 7, 2016 at 17:28

2 Answers 2

1

Your question was not clear!

So you want to get entries with tag having 3 AND 2 AND 11.

You'll have to split your string/filter 3,2,11, then generate the condition using a loop:

$sql = "SELECT * FROM table1 WHERE bTown = ?";

foreach (explode(",", $FilterTag) as $value)
    $sql .= " AND REGEXP_LIKE(tag, '\b$value\b')";

which should produce the following query:

SELECT * FROM table1 WHERE bTown = ? 
    AND REGEXP_LIKE(tag, '\b3\b')
    AND REGEXP_LIKE(tag, '\b2\b')
    AND REGEXP_LIKE(tag, '\b11\b')

Extras:

To allow only numbers from 1 to 99, use this regex:

\b[1-9][0-9]?\b

DEMO

To allow two digits numbers (with leading zeros for numbers <10) use:

\b(0[1-9]|[1-9][0-9])\b

DEMO

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

1 Comment

what i'm wanting is to compare a set of numbers, the $FilterTag string will be different each time and i want to match those numbers in my database.
0

Returns all records with 2 digits.

select * from table1
where tag REGEXP ('^[0-9]{2}$');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.