1

I have following records in mysql(5.7) table in which data has json data type.

id  data 
1   [{"key": 1,"value": "rom"},{"key": 54,"x": 1}]
2   [{"key": 1,"value": "sun"},{"key": 54,"x": 0.5}]
3   [{"key": 54,"x": 1.2},{"key": 1,"value": "test"}]
4   [{"key": 1,"value": "japan"},{"key": 54,"x": 2}]
5   [{"key": 1,"value": "east"},{"key": 52,"x": 1}]

I want to make where condition on data column. if key = 54 and x >= 1 then it should retrun id => 1,3,4

if key = 54 and x < 1 then it should retrun id => 2

2
  • Are you using MySQL version 8+ ? Commented Nov 28, 2018 at 12:00
  • Mysql Version is 5.7 Commented Nov 28, 2018 at 12:02

1 Answer 1

1

Dealing with arrays is a little messy in cases like this. You end up having to do positional things like the follow that, while they do work, are not easy to read or modify. JSON objects are a little more flexible.

select id from y where 
(json_contains(doc,"54","$[0].key") or json_contains(doc,'54',"$[1].key")
AND
(json_extract(doc,"$[0].x") >= 1 or json_extract(doc,"$[1].x") >= 1)
);
Sign up to request clarification or add additional context in comments.

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.