简体   繁体   English

Mysql 5.7.27:搜索 JSON 数组

[英]Mysql 5.7.27: Searching JSON array

I'm trying to figure out how to search a table's JSON array column.我试图弄清楚如何搜索表的 JSON 数组列。 I found this Stackoverflow question but I'm unclear on how JSON_SEARCH works.我发现了这个 Stackoverflow 问题,但我不清楚 JSON_SEARCH 是如何工作的。

In the answers, people seem to be giving JSON_SEARCH direct data to search through:在答案中,人们似乎在给 JSON_SEARCH 直接数据进行搜索:

SELECT JSON_SEARCH('["1","2","3","4","5"]', 'one', "2")

But since I'm making a search I don't actually have the array ["1","2","3","4","5"]但由于我正在搜索,我实际上并没有数组["1","2","3","4","5"]

I'm creating a search query like so:我正在创建一个搜索查询,如下所示:

SELECT * FROM `server_list` 
WHERE
    `server_version` LIKE '%%'
AND
    `server_name` LIKE '%%'
AND 
    `server_slots` > 1500
AND
    `server_slots` < 4000

How can I also search for specific tags within that same query?我怎样才能在同一个查询中搜索特定标签? Something along:伴随着一些东西:

AND
    JSON_SEARCH(`server_tags`, ['1', '2', '3'])

Where server_tags is the column I want to search and ['1', '2', '3'] are multiple tags I want to search for.其中server_tags是我要搜索的列, ['1', '2', '3']是我要搜索的多个标签。

Edit: I just realised JSON columns are just strings, which allows me to do this:编辑:我刚刚意识到 JSON 列只是字符串,这允许我这样做:

SELECT * FROM `lista_server` 
    `server_tags` LIKE '%value%'
    OR `server_tags` LIKE '%value2%'

Is there anything wrong with this method?这种方法有什么问题吗?

If you want to check for the existence of any of the tags, you can OR together the results of JSON_SEARCH for each of them:如果您想检查任何标签是否存在,您可以将每个标签的JSON_SEARCH的结果OR在一起:

AND (JSON_SEARCH(server_tags, 'one', '1') IS NOT NULL OR
     JSON_SEARCH(server_tags, 'one', '2') IS NOT NULL OR
     JSON_SEARCH(server_tags, 'one', '3') IS NOT NULL
    )

To check they all exist, just AND the searches together:要检查它们存在AND只需将搜索组合在一起:

AND (JSON_SEARCH(server_tags, 'one', '1') IS NOT NULL AND
     JSON_SEARCH(server_tags, 'one', '2') IS NOT NULL AND
     JSON_SEARCH(server_tags, 'one', '3') IS NOT NULL
    )

Note attempting to search as text will likely not work, it's possible the values in the JSON array will not be in the same order as in your test string (eg ['1', '2'] won't match ['2', '1'] ), or the length of the JSON array could be different (eg ['1', '2'] won't match ['1', '2', '3'] ) etc.注意尝试搜索文本可能不起作用,JSON 数组中的值可能与您的测试字符串中的顺序不同(例如['1', '2']不会匹配['2', '1'] ),或者 JSON 数组的长度可能不同(例如['1', '2']不会匹配['1', '2', '3'] )等。

The problem with this method:这种方法的问题:

SELECT * FROM `lista_server` 
WHERE `server_tags` LIKE '%value%'
OR `server_tags` LIKE '%value2%'

Is that the wildcard-matching doesn't know anything about the value boundaries.是通配符匹配对值边界一无所知。

Example:例子:

INSERT INTO lista_server SET server_tags = '112,456,789';

Then search for tag '1' using your method:然后使用您的方法搜索标签“1”:

SELECT * FROM lista_server WHERE server_tags LIKE '%1%'

It matches.它匹配。 Because 112 is matched by '%1%'.因为 112 与 '%1%' 匹配。

This is one of the many reasons it's trouble to store a comma-separated list of values when you want to search for individual values in the list.这是当您想在列表中搜索单个值时存储逗号分隔的值列表很麻烦的众多原因之一

Using JSON is just a troublesome as using a comma-separated list.使用 JSON 就像使用逗号分隔的列表一样麻烦。

Instead, if you want to search for individual values, you need to normalize your table.相反,如果要搜索单个值,则需要对表进行规范化。 Storing tags one per row in a child table:在子表中每行存储一个标签:

INSERT INTO lista_server_tags (lista_server_id, tag) 
VALUES (1234, '112'), (1234, '456'), (1234, '789');

Then you can search for them as individual values:然后您可以将它们作为单独的值进行搜索:

SELECT l.* FROM lista_server AS l 
JOIN lista_server_tags AS t ON (l.id = t.lista_server_id)
WHERE t.tag = '112'

And you are sure it won't match incorrectly.而且您确定它不会匹配错误。 It will only match the one value, not values that coincidentally contain the string you search for.它只会匹配一个值,而不是巧合包含您搜索的字符串的值。 You can also use an index to help speed up the search.您还可以使用索引来帮助加快搜索速度。 You can add an unlimited number of tags.您可以添加无限数量的标签。 And many other benefits.以及许多其他好处。

Absolutely nothing is wrong with what you did, just make sure your search term is clean to prevent (SQL Injection).你所做的绝对没有错,只要确保你的搜索词是干净的以防止(SQL 注入)。

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

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