1

I am very much struggling in MYSQL when it comes to query JSON type column, this time I need to put where condition on a JSON type field.

JSON type value in my user table is like this: {"id": ["2", "4"]}

When I query a SELECT on my table like:

SELECT json_extract(gids, '$.id') from users

Its outputs like this:

["2", "4"]

I am required to write a query where I need to get all users where id=2

I have tried somthing like this:

SELECT *
FROM users
WHERE json_extract(gids, '$.id')="2"

But its not working, Please help on this as I am stuck here.

Thanks

3
  • 1
    WHERE JSON_OVERLAPS(JSON_EXTRACT(gids, '$.id'), '["2"]')? Commented May 22, 2020 at 19:15
  • Hey, I am getting [42000][1305] FUNCTION <DBName>.JSON_OVERLAPS does not exist, any suggestions I am having mysql 5.7 in both local and server Commented May 22, 2020 at 19:22
  • JSON_OVERLAPS is in MySQL 8.0 Commented May 27, 2020 at 14:34

1 Answer 1

2

you can try using JSON_SEARCH with JSON_EXTRACT

JSON_SEARCH will search data inside array.

SELECT *
FROM users
WHERE JSON_SEARCH(JSON_EXTRACT(test, '$.id'), 'one', "1") IS NOT NULL

you can check https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#function_json-search this link for reference.

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

2 Comments

have tested this and its working fine with one Id is it possible to match multiple ids like JSON_SEARCH(JSON_EXTRACT(test, '$.id'), 'one', "1,2,3") IS NOT NULL
haven't tried that and not sure if that is possible will check about that if i can found something then will let you know about that

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.