M
M
m1kz2021-08-31 14:27:34
MySQL
m1kz, 2021-08-31 14:27:34

How to find row in mysql where json array value is equal to value?

How to find something like:
SELECT * FROM test WHERE array_test.value = 2;
and that it displays a row with id=2;

mysql> select * from test;
+----+----------------------------+
| id | array_test                 |
+----+----------------------------+
|  2 | [1, 2, "asd", true]        |
|  3 | {"0": "false", "1": "one"} |
+----+----------------------------+
2 rows in set (0,00 sec)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Akina, 2021-08-31
@Akina

As a demonstration that "The server is smarter than you think!":

SELECT id, CAST(array_test AS CHAR) array_test 
FROM test
WHERE JSON_OVERLAPS(array_test, CAST('[2]' AS JSON));

DEMO fiddle
PS. Yes, there is a comma there. And the exclamation mark.

D
Dmitry, 2021-08-31
@thewind

https://dev.mysql.com/doc/refman/8.0/en/json.html#...

Q
qqNemo, 2021-08-31
@qqNemo

This is a search on the array_test column, if "2", "yvayv2", "utsatsu2autsats" hits. Will output them to the result
SELECT * FROM `test` WHERE `array_test` LIKE '%2%'

L
Lazy @BojackHorseman MySQL, 2021-08-31
Tag

Oh, 1NF, where are you ((

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question