How to select one value of MySQL JSON array

Issue

I am trying to display everything that contains zbs tag2 from a JSON format that is in my MariaDB see screen.

So my query to add the values is owner varhcarm picture TEXT and tags JSON:

INSERT INTO json_pics(owner, picture, tags) VALUES ("test", "test.png",'["tag3", "tag4"]');

I’m trying to display all entries that have e.g. "tag4" in field tags with a SELECT statement as mentioned above. So that I can filter for one of the things in the brackets. This should work with JSON_CONTAINS or something like that? Image from the example DB: enter image description here

Now id 2 and 3 should be displayed because they both have tag 4 🙂

Thanks in advance and many greetings, Flo

Solution

I’m trying to display all entries that have e.g. "tag4" in field tags with a SELECT statement

SELECT * 
FROM json_pics
WHERE JSON_CONTAINS(tags, '"tag4"');

Answered By – Akina

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published