1

Hello i have a question about mysql json_extract function.

there are two tables;

users
+------------------------------------+
|   id  |  email                     |
+------------------------------------+
|   1   |  [email protected]            |      
+------------------------------------+
|   2   |  [email protected]          |      
+------------------------------------+

room
+----------------------------------------------------------------------------------+
|   id  |  invited(json type)                                                      |                                          
+----------------------------------------------------------------------------------+
|   1   |  {"invite": [{"To": "[email protected]"}, {"To": "[email protected]"}]}    
+----------------------------------------------------------------------------------+

i want to get invited users from room table. so i tried

SELECT A.id, A.email
FROM users A JOIN room B ON 
(A.email = json_extract(B.invited,'$.invite[*].To')

but json_extract() return

["[email protected]", "[email protected]"]

If I use 'json_extract(B.invited,'$.invite[0].To')'
it is possible to get one. but can't get several users.

is there any possible way?

0

1 Answer 1

1

Try it with JSON_CONTAINS. It should be something like that:

SELECT A.id, A.email
FROM users A
JOIN room B 
  ON json_contains(json_extract(B.invited,'$.invite[*].To'), json_quote(A.email))
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.