1

I have following values in my mysql table column named color: 1;2;3;11;12

id | color
1  | 1;2
2  | 2;11
3  | 1;3
4  | 12

I want to use REGEXP to select only those rows from the table which have color 1.

When i use simple expression

color REGEXP '1'

it also selects rows which have values 11 or 12 whereas i am looking for those with value 1 only. So when i use above expression it show me all 4 rows as shown in the example above where as i want it to show me rows with id 1 and 3 only.

2
  • 2
    Your database is violates the first normal form. You are saving multiple values in one column (color). Please normalize your database. Commented Aug 10, 2011 at 7:27
  • 1
    I think this is not the point. Commented Aug 10, 2011 at 7:29

2 Answers 2

1

You need to use word-delimiters on either side of the 1 to isolate it:

color REGEXP '[[:<:]]1[[:>:]]'
Sign up to request clarification or add additional context in comments.

Comments

0

You could try something like

REGEXP '^1;|;1;|;1$'

If I'm not wrong this will either find a 1 at the start followed by a semicolon or a 1 between two semicola, or a single 1 after a semicolon. This should pretty much be it.

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.