S
S
swaro2020-09-03 15:32:59
SQL
swaro, 2020-09-03 15:32:59

How to search by JSON in MariaDB?

Hello! I have a table with records in my database (MariaDB), each record has a field with tags. The tags are written as a JSON array. It looks something like this:

id | tags
---+-------
1  | ["first", "second", "third"]
2  | ["first", "target", "second", "third"]
3  | ["foo", "bar"]

So, I need to select all records that, for example, will have "target" and "first" tags in the "tags" field. So that the result looks like this:
id | tags
---+-------
2  | ["first", "target", "second", "third"]

Question: How can this be done? And can I have an example?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
display: block, 2020-09-03
@qork

https://mariadb.com/kb/en/json_search/

G
Genri_Rus, 2021-08-14
@Genri_Rus

As far as I understand, it can be done like this:

"SELECT * FROM `table_name` WHERE JSON_CONTAINS(tags, '{\"target\", \"first\"}')"

The version of MariaDB that supports these features must be at least 10.2.3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question