1

I got the two following tables

In the 'items' table, desc column is a JSON array

Table items

+--------+--------+-------+
|item_id | name   | json  |
+--------+--------+-------+
| 1      | Item 1 | [1,2] |
+--------+--------+-------+

Table descriptions

+----+-------------+
| id | description |
+----+-------------+
| 1  | First desc  |
+----+-------------+
| 2  | Second desc |
+----+-------------+

I would like to retrieve all the descriptions for a prticular id.

I have tried this, with no luck:

SELECT id, description 
FROM descriptions 
WHERE id IN (SELECT JSON_EXTRACT(json, '$[*]') 
             FROM items 
             WHERE item_id=1)
1

1 Answer 1

1

You can use JSON function json_search(), which is available from MySQL 5.7 onwards:

select i.item_id, i.name, d.id, d.description
from items i
inner join descriptions d on json_search(i.json, 'one', d.id) is not null 
where i.item_id = 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.