114

我已经在 mysql DB 中插入了json encoded数据类型的记录,现在我必须在json encoded数据中进行搜索,但我无法使用以下 MySql 查询获取正确的数据。

SELECT  `id` ,  `attribs_json` 
FROM  `products` 
WHERE  `attribs_json` REGEXP  '"1":{"value":[^"3"$]'

查询结果键等于“1”,值是除“3”以外的任何值

我的数据是:

{"feature":{"1":{"value":"["2","3"]"},
            "2":{"value":["1"]},
            "5":{"value":""},
            "3":{"value":["1"]},
            "9":{"value":""},
            "4":{"value":"\u0633\u0627\u062a\u0646"},
            "6":{"value":""},
            "7":{"value":""},
            "8":{"value":""}
           },
"show_counter":"0",
"show_counter_discount":""
}}
4

10 回答 10

199

如果你的MySQL 版本 >= 5.7,那么你可以试试这个:

SELECT JSON_EXTRACT(name, "$.id") AS name
FROM table
WHERE JSON_EXTRACT(name, "$.id") > 3

输出:

+-------------------------------+
| name                          | 
+-------------------------------+
| {"id": "4", "name": "Betty"}  | 
+-------------------------------+


有关详细信息,请查看 MySQL 参考手册:
https ://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html

于 2016-08-26T10:17:48.640 回答
32

如果您使用的是 MySQL 最新版本,则以下可能有助于满足您的要求。

select * from products where attribs_json->"$.feature.value[*]" in (1,3)
于 2016-09-01T12:10:53.650 回答
12
  1. 在数据库中存储 JSON 违反了第一范式。

    您可以做的最好的事情是将特征标准化并存储在另一个表中。然后,您将能够使用具有更好外观和性能的连接查询。您的 JSON 甚至类似于表格。

  2. Mysql 5.7 具有内置 JSON 功能: http:
    //mysqlserverteam.com/mysql-5-7-lab-release-json-functions-part-2-querying-json-data/

  3. 正确的模式是:

    WHERE  `attribs_json` REGEXP '"1":{"value":[^}]*"3"[^}]*}'
    

    [^}]将匹配任何字符,除了}

于 2015-05-23T10:57:42.917 回答
11

我使用这个查询

SELECT id FROM table_name WHERE field_name REGEXP '"key_name":"([^"])key_word([^"])"';
or
SELECT id FROM table_name WHERE field_name RLIKE '"key_name":"[[:<:]]key_word[[:>:]]"';

我使用它来搜索部分值的第一个查询。第二个查询我用它来搜索确切的单词。

于 2015-08-04T21:59:11.303 回答
8

对于Mysql8->

询问:

SELECT properties, properties->"$.price" FROM book where isbn='978-9730228236' and  JSON_EXTRACT(properties, "$.price") > 400;

数据:

mysql> select * from book\G;
*************************** 1. row ***************************
id: 1
isbn: 978-9730228236
properties: {"price": 44.99, "title": "High-Performance Java Persistence", "author": "Vlad Mihalcea", "publisher": "Amazon"}
1 row in set (0.00 sec)
于 2020-12-28T03:00:17.813 回答
5

如果 MySQL 版本 < 5.7

SELECT fields
FROM table
WHERE field_json LIKE '%"key":"70"%';

// 70 = 价值

于 2021-04-02T14:32:03.767 回答
4

请这样做。

SELECT * FROM `products` 
    WHERE JSON_UNQUOTE(JSON_EXTRACT(`attribs_json`, '$.value')) LIKE '%X%'
于 2021-05-08T09:31:41.870 回答
1
SELECT
    country.NAME AS 'country_name',
    city.NAME AS 'city_name',
    city.district,
    city.info,
    JSON_EXTRACT( city.info, "$.Population" ) AS 'formated_population' 
FROM
    city
    INNER JOIN country ON city.CountryCode = country.
    CODE INNER JOIN countrylanguage ON country.CODE = countrylanguage.CountryCode 
GROUP BY
    city.NAME,
    city.district,
    country.NAME;

ORDER BY
    country.NAME ASC;

在此处输入图像描述

于 2021-11-10T10:24:17.203 回答
-4

我认为...

搜索部分值:

SELECT id FROM table_name WHERE field_name REGEXP '"key_name":"([^"])*key_word([^"])*"';

搜索确切的词:

SELECT id FROM table_name WHERE field_name RLIKE '"key_name":"[[:<:]]key_word[[:>:]]"';
于 2020-01-28T06:46:26.177 回答
-6

对于 MySQL 全部(和 5.7)

SELECT LOWER(TRIM(BOTH 0x22 FROM TRIM(BOTH 0x20 FROM SUBSTRING(SUBSTRING(json_filed,LOCATE('\"ArrayItem\"',json_filed)+LENGTH('\"ArrayItem\"'),LOCATE(0x2C,SUBSTRING(json_filed,LOCATE('\"ArrayItem\"',json_filed)+LENGTH('\"ArrayItem\"')+1,LENGTH(json_filed)))),LOCATE(0x22,SUBSTRING(json_filed,LOCATE('\"ArrayItem\"',json_filed)+LENGTH('\"ArrayItem\"'),LOCATE(0x2C,SUBSTRING(json_filed,LOCATE('\"ArrayItem\"',json_filed)+LENGTH('\"ArrayItem\"')+1,LENGTH(json_filed))))),LENGTH(json_filed))))) AS result FROM `table`;
于 2018-08-27T16:15:10.573 回答