Jordi Huertas
Jordi Huertas

Reputation: 93

Search for a value in a JSON field in MYSQL

I am trying to understand the "new" MYSQL JSON field.

I have this table:

id (int-11, not_null, auto_inc)
customer_id (int-11, not null)
labels (json)

With this data:

id: 1
customer_id: 1
labels: [{"isnew": "no", "tagname": "FOO", "category": "CAT_1", "isdeleted": "no"}, {"isnew": "yes", "tagname": "BAR", "category": "CAT_2", "isdeleted": "no"}]

JSON beautify

[
  {
    "tagname": "FOO",
    "category": "CAT_1",
    "isnew": "no",
    "isdeleted": "no"
  },
  {
    "tagname": "BAR",
    "category": "CAT_2",
    "isnew": "yes",
    "isdeleted": "no"
  }
]

And now I want to SELECT all the customers (by customer_id) in the table that have a specific category and a specific tagname

I tried this one:

SELECT * FROM labels_customers_json
WHERE JSON_SEARCH(labels, 'all', 'BAR') IS NOT NULL

But this is not what I want. This one is searching in every json attribute. I have seen some examples of JSON_EXTRACT:

SELECT * FROM `e_store`.`products`
WHERE
    `category_id` = 1
    AND JSON_EXTRACT(`attributes` , '$.ports.usb') > 0
    AND JSON_EXTRACT(`attributes` , '$.ports.hdmi') > 0;

SELECT c, c->"$.id", g, n
FROM jemp
WHERE JSON_EXTRACT(c, "$.id") > 1
ORDER BY c->"$.name";

So I tried this

SELECT * FROM labels_customers_json
WHERE JSON_EXTRACT(labels, '$.tagname') = 'BAR'

SELECT labels, JSON_EXTRACT(labels, "$.customer_id"), customer_id
FROM labels_customers_json
WHERE JSON_EXTRACT(labels, "$.customer_id") > 0

Upvotes: 1

Views: 447

Answers (2)

Sebastian Brosch
Sebastian Brosch

Reputation: 43574

You can use JSON_SEARCH to search on a specific path too. So you can use the following query:

SELECT * 
FROM labels_customers_json 
WHERE JSON_SEARCH(labels, 'all', 'BAR', NULL, '$[*].tagname') IS NOT NULL

You can also use JSON_EXTRACT and JSON_CONTAINS together:

SELECT *
FROM labels_customers_json
WHERE JSON_CONTAINS(JSON_EXTRACT(labels, '$[*].tagname'), '["BAR"]') > 0;

You can also use only JSON_CONTAINS to check:

SELECT *
FROM labels_customers_json
WHERE JSON_CONTAINS(labels, '{"tagname":"BAR"}') > 0;

demos: https://www.db-fiddle.com/f/rufrThAQPfXHrK9YyibFSm/2

Upvotes: 1

Halko Karr-Sajtarevic
Halko Karr-Sajtarevic

Reputation: 2268

You could probably try using SELECT * FROM labels_customers_json WHERE JSON_SEARCH(labels, 'all', "BAR", NULL, "$[*].tagname") is not null - although i cannot say if that is the best way to perform this query.

Upvotes: 2

Related Questions