2

This one is bit complicated than sinmple range in mysql , my field data is {"id":"15","value":"1200"} and I can find it by ,

SELECT * 
FROM  `my_table` 
WHERE  `my_field` 
REGEXP  '{"id":"15","value":"1200"}'

but what I need is the range for the value

so I need to look for range between 1000 > 2000 and field with json value 1200 should match. I cant redo the output with php because it will eat up the resource , so please if it is possible within Mysql without me processing the data trough php on return

any help is appreciated!

SOLUTION TO ANYONE !

LC gave us very nice solution

REGEXP '{"id":"15","value":"(1[0-9][0-9][0-9]|2000)"}'

1 Answer 1

5

My knee-jerk reaction is to say "stop storing more than one value in a single column", but if you have to do it that way and you have regex power, use it (untested):

SELECT * 
FROM `my_table` 
WHERE `my_field` REGEXP '{"id":"15","value":"(1[0-9][0-9][0-9]|2000)"}'

Keep in mind, there's no way to optimize this query using indexes so you are stuck with examining every row in my_table.

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

5 Comments

im getting Got error 'repetition-operator operand invalid' from regexp
got it! need dot . infront ?
Sorry, was missing the : in the (?: for the non-capturing group. But no, you shouldn't need a dot in front, and that would probably make it interpret the curly-brace as a repetition operator.
for m eit works only if there is . or : INfont of ? not after
Right. Non-capturing groups mean nothing in mysql because there's no place to return it, so all groups are non-capturing and the (?: syntax is not implemented. stackoverflow.com/questions/7119338/…

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.