1

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
4
  • what version of mysql do you use? Commented Jul 27, 2018 at 10:31
  • Just installed last wamp version: mysqlnd 5.0.11-dev Commented Jul 27, 2018 at 10:33
  • Then anyone experienced on this field? Commented Jul 27, 2018 at 15:33
  • It has the correct version now, 5.7.22. My phpmyadmin was obsolete, it is 4.8.2, but still having the same problem Commented Jul 31, 2018 at 9:44

2 Answers 2

2

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.

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

3 Comments

Like this? SELECT * FROM labels_customers_json WHERE "TEST" IN JSON_EXTRACT (labels, "$[*].tagname"). It returns a syntax error near 'JSON_EXTRACT...
It works, but you both answer the same. I do not know who was first to give the reptutation.
according to the timestamps, mine was 4 seconds before, but sebastians is more detailed
1
+50

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

4 Comments

It works, but you both answer the same. I do not know who was first to give the reptutation.
@JordiHuertas that have a specific category and a specific tagname - on the same object or can be different objects?
It can have different. This is a tag system to label the customers. So a tagname can have a different category in many customers. For example, it should let me find a customer that have a label "unsuscribed" from a category "email" or "postal_mail".
I was doing this saving JSON in a TEXT field, but now is so complicated to work all the JSON with PHP.

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.