1
WHERE FIND_IN_SET(1, JSON_UNQUOTE(JSON_EXTRACT(mycolumn, '$.parent[0].child'))) > 0

this query is working

WHERE FIND_IN_SET(1, JSON_UNQUOTE(JSON_EXTRACT(mycolumn, '$.parent[*].child'))) > 0

this query is not working

but when i want to put * instead of 0 my query is not working. i want to search key in all positions. first query only shows "search key" equal to only first (0) json

this is my json in mysql column

{"parent":[ { "child":"1,3", "type":"" },{ "child":"36,41", "type":"1" },{ "child":"52", "type":"1" } ]}
4
  • The latter call probably does not return a single comma separated list of values. Commented Jul 15, 2020 at 7:20
  • question is edited. last call does not return anything Commented Jul 15, 2020 at 7:33
  • What does it mean the query is not working? Does it throws some error? Or the output is not expected? What is search key? Won't JSON_CONTAINS be a better fit for you here? Commented Jul 15, 2020 at 7:58
  • question is edited. search key is what i want to search, for exampe i want to search 1 Commented Jul 15, 2020 at 8:18

1 Answer 1

3

You can use a solution like this:

SELECT * 
FROM table_name
WHERE FIND_IN_SET(1, REGEXP_REPLACE(JSON_EXTRACT(column_name, '$.parent[*].child'), '[\\[\\]\\" ]', '')) > 0

JSON_EXTRACT returns a single value or a JSON array with all found values. The JSON array is not valid to use on FIND_IN_SET. You first have to remove the [, ] and " to get the comma separated list of all found values.

You can also use JSON_SEARCH to solve this:

SELECT *
FROM table_name
WHERE JSON_SEARCH(column_name, 'one', '%,1,%', NULL, '$.parent[*].child') IS NOT NULL OR
  JSON_SEARCH(column_name, 'one', '1,%', NULL, '$.parent[*].child') IS NOT NULL OR
  JSON_SEARCH(column_name, 'one', '%,1', NULL, '$.parent[*].child') IS NOT NULL

demo on dbfiddle.uk

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.