简体   繁体   English

如何在MySQL中查找JSON数组?

[英]How to search JSON array in MySQL?

Let's say I have a JSON column named data in some MySQL table, and this column is a single array .假设我在某个 MySQL 表中有一个名为data的 JSON 列,并且该列是一个数组 So, for example, data may contain:因此,例如,数据可能包含:

[1,2,3,4,5] [1,2,3,4,5]

Now I want to select all rows which have a data column where one of its array elements is greater than 2. Is this possible?现在我想要 select 所有具有数据列的行,其中一个数组元素大于 2。这可能吗?

I tried the following, but seems it is always true regardless of the values in the array:我尝试了以下方法,但无论数组中的值如何,它似乎总是正确的:

SELECT * from my_table
WHERE JSON_EXTRACT(data, '$[*]') > 2;

You may search an array of integers as follows:您可以按如下方式搜索整数数组:

  JSON_CONTAINS('[1,2,3,4,5]','7','$') Returns: 0
  JSON_CONTAINS('[1,2,3,4,5]','1','$') Returns: 1

You may search an array of strings as follows:您可以按如下方式搜索字符串数组:

  JSON_CONTAINS('["a","2","c","4","x"]','"x"','$') Returns: 1
  JSON_CONTAINS('["1","2","3","4","5"]','"7"','$') Returns: 0

Note: JSON_CONTAINS returns either 1 or 0注意:JSON_CONTAINS 返回 1 或 0

In your case you may search using a query like so:在您的情况下,您可以使用如下查询进行搜索:

SELECT * from my_table
WHERE JSON_CONTAINS(data, '2', '$');
SELECT JSON_SEARCH('["1","2","3","4","5"]', 'one', "2") is not null 

is true是真的

SELECT JSON_SEARCH('["1","2","3","4","5"]', 'one', "6") is not null

is false是假的

I don't know if we found the solution.我不知道我们是否找到了解决方案。 I found with MariaDB a way, to search path in a array.我用 MariaDB 找到了一种在数组中搜索路径的方法。 For example, in array [{"id":1}, {"id":2}] , I want find path with id equal to 2.例如,在数组[{"id":1}, {"id":2}] ,我想找到 id 等于 2 的路径。

SELECT JSON_SEARCH('name_field', 'one', 2, null, '$[*].id')
FROM name_table

The result is:结果是:

"$[1].id"

The asterisk indicate searching the entire array星号表示搜索整个数组

Since MySQL 8 there is a new function called JSON_TABLE . 从 MySQL 8 开始,有一个名为JSON_TABLE的新函数。

CREATE TABLE my_table (id INT, data JSON);

INSERT INTO my_table VALUES 
  (1, "[1,2,3,4,5]"), 
  (2, "[0,1,2]"), 
  (3, "[3,4,-10]"), 
  (4, "[-1,-2,0]");

SELECT DISTINCT my_table.* 
FROM my_table, JSON_TABLE(data, "$[*]" COLUMNS(nr INT PATH '$')) as ids 
WHERE ids.nr > 2;

+------+-----------------+
| id   | data            |
+------+-----------------+
|    1 | [1, 2, 3, 4, 5] |
|    3 | [3, 4, -10]     |
+------+-----------------+
2 rows in set (0.00 sec)

A possible way is to deal with the problem as string matching.一种可能的方法是将问题作为字符串匹配来处理。 Convert the JSON to string and match.将 JSON 转换为字符串并匹配。

Or you can use JSON_CONTAINS .或者您可以使用JSON_CONTAINS

You can use JSON extract to search and select data您可以使用 JSON 提取来搜索和选择数据

SELECT data, data->"$.id" as selectdata
FROM table
WHERE JSON_EXTRACT(data, "$.id") = '123'
#ORDER BY c->"$.name";
limit 10 ;

我使用 JSON_EXTRACT 和 JSON_CONTAINS (MariaDB) 的组合:

SELECT * FROM table WHERE JSON_CONTAINS(JSON_EXTRACT(json_field, '$[*].id'), 11, '$');
SET @doc = '[{"SongLabels": [{"SongLabelId": "111", "SongLabelName": "Funk"}, {"SongLabelId": "222", "SongLabelName": "RnB"}], "SongLabelCategoryId": "test11", "SongLabelCategoryName": "曲风"}]';
SELECT *,  JSON_SEARCH(@doc, 'one', '%un%', null, '$[*].SongLabels[*].SongLabelName')FROM t_music_song_label_relation;

result: "$[0].SongLabels[0].SongLabelName"结果:“$[0].SongLabels[0].SongLabelName”

SELECT song_label_content->'$[*].SongLabels[*].SongLabelName' FROM t_music_song_label_relation;

result: ["Funk", "RnB"]结果:[“放克”,“RnB”]

I have similar problem, search via function我有类似的问题,通过函数搜索

create function searchGT(threshold int, d JSON)
returns int
    begin
        set @i = 0;
        while @i < json_length(d) do
            if json_extract(d, CONCAT('$[', @i, ']')) > threshold then
                return json_extract(d, CONCAT('$[', @i, ']'));
            end if;
            set @i = @i + 1;
        end while;
        return null;
    end;

select searchGT(3, CAST('[1,10,20]' AS JSON));

This example works for me with mysql 5.7 above这个例子适用于我上面的 mysql 5.7

SET @j = '{"a": [ "8428341ffffffff", "8428343ffffffff", "8428345ffffffff", "8428347ffffffff","8428349ffffffff", "842834bffffffff", "842834dffffffff"], "b": 2, "c": {"d": 4}}';
select JSON_CONTAINS(JSON_EXTRACT(@j , '$.a'),'"8428341ffffffff"','$') => returns 1
             

notice about " around search keyword, '"8428341ffffffff"'注意"围绕搜索关键字, '"8428341ffffffff"'

This seems to be possible with to JSON_TABLE function. It's available in mysql version 8.0 or mariadb version 10.6.这似乎可以用于JSON_TABLE function。它在 mysql 版本 8.0 或 mariadb 版本 10.6 中可用。

With this test setup使用此测试设置

CREATE TEMPORARY TABLE mytable
WITH data(a,json) AS (VALUES ('a','[1]'),
                             ('b','[1,2]'),
                             ('c','[1,2,3]'),
                             ('d','[1,2,3,4]'))
SELECT * from data;

we get the following table我们得到下表

+---+-----------+
| a | json      |
+---+-----------+
| a | [1]       |
| b | [1,2]     |
| c | [1,2,3]   |
| d | [1,2,3,4] |
+---+-----------+

It's possible to select every row from mytable wich has a value greater than 2 in the json array with this query. select mytable 中的每一行都可以使用此查询在 json 数组中具有大于 2 的值。

SELECT * FROM mytable
WHERE TRUE IN (SELECT val > 2
               FROM JSON_TABLE(json,'$[*]'
                            columns (val INT(1) path '$')
                    ) as json
              )

Returns:退货:

+---+-----------+
| a | json      |
+---+-----------+
| c | [1,2,3]   |
| d | [1,2,3,4] |
+---+-----------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM