1

I am storing the json array in the mysql field called member_details,

[{"religion":null,"caste":["2","3","4"],"sub_caste":["0","1","2"],"family_value":null,"family_status":null}]    

Now, I want to search for the members who are having caste of 3. Some times I need to search for sub_caste 2.

How can i achieve that?

I have tried to use json_contains but it is giving me empty results

select * from member where json_contains('member_details', '{"caste" : "2"}') 

but it gives empty result.

2 Answers 2

1

Here is one way:

SELECT *
FROM yourTable
WHERE JSON_SEARCH(member_details->"$[0].caste", 'one', '3') IS NOT NULL;

The syntax member_details->"$[0].caste first accesses the first (and only) JSON element in the outer array, using [0]. Then, it accesses the caste key, using .caste. Finally, JSON_SEARCH checks if the value 3 occurs within the caste array.

If you needed to search for sub_caste 2 then you could use similar logic.

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

Comments

0
  • select * from member where JSON_EXTRACT(caste, "2");

Can you please try this query for getting values from DB. Reference Link

I hope this helps you.

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.