1

Lets say I have a JSON column json with the following data:

+----+-----------------+
| id | json            |
+====+=================+
| 1  | [               |
|    |   {             |
|    |     "type":"a", |
|    |     "id":11111  |
|    |   },            |
|    |   {             |
|    |     "type":"a", |
|    |     "id":22222  |
|    |   },            |
|    |   {             |
|    |     "type":"b", |
|    |     "id":11111  |
|    |   }             |
|    | ]               |
+----+-----------------+
| 2  | [               |
|    |   {             |
|    |     "type":"b", |
|    |     "id":11111  |
|    |                 |
|    |   },            |
|    |   {             |
|    |     "type":"b", |
|    |     "id":22222  |
|    |                 |
|    |   },            |
|    |   {             |
|    |     "type":"c", |
|    |     "id":11111  |
|    |   }             |
|    | ]               |
+----+-----------------+

Is there a way to determine if there is an entry in this array which has type = a AND id = 11111 (in the same object), such that ROW 1 would be returned but not ROW 2?

What i've tried is something like

SELECT * 
  FROM table 
 WHERE json->>"$[*].type" = "a" 
   AND json->>"$[*].id" = 11111 

but that's not for the same entry.

1 Answer 1

2

Ofcourse I didn't stop looking - and found the solution after 3 hours of trail and error. Feeling a little stupid now I know the solution.

SELECT *
  FROM table
 WHERE JSON_CONTAINS(json, JSON_OBJECT('type', 'a', 'id', 11111));
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.