0

I'm storing permissions into DB with Array JSON String, and i want select them by permission specific permission. at this time I'm selecting them like this:

  1 | Dog   | [3,4]
  2 | Cat   | [33,4]
  3 | Tiger | [5,33,4]
  4 | wolf  | [3,5]


 SELECT * FROM `pages` WHERE access REGEXP '([^"])3([^"])'

it works but not as it should work. This query gives me all records which contains 3 but also it gives which contains 33. my question is how i must format my regexp to get row by specific value into json string.

p.s i have mysql 5.5 so as i know on this version json functions is not supported

3
  • Possible duplicate of check if a value exists in json encode array in mysql Commented Jan 4, 2017 at 15:33
  • @e4c5 i have 5.5 mysql version Commented Jan 4, 2017 at 15:37
  • Like the duplicate says you shouldn't be using JSON if oyu are on mysql 5.5 you are going to have all sorts of problems with this. Either upgrade or ditch this design. Read up on 'database normalization' Commented Jan 5, 2017 at 1:01

2 Answers 2

1

If you only have numbers in the fields, you can alter your regexp to only take values where the string you are looking for (here the '3') does not have another number immediately close to it :

SELECT * FROM `pages` WHERE access REGEXP '([^"0-9])3([^"0-9])'    
Sign up to request clarification or add additional context in comments.

Comments

0
REGEXP '[[:<:]]3[[:>:]]'

That is, use the "word boundary" thingies.

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.