【问题标题】:How can write queries in MySQL that can parse JSON data in a column?如何在 MySQL 中编写可以解析列中 JSON 数据的查询?
【发布时间】:2015-05-22 04:09:34
【问题描述】:

我在 MySQL 中有一个表,其中有一列存储 JSON 对象。 如何轻松运行可以在 WHERE 子句中包含某些 JSON 字段的查询?

前: 有一个名为articles的表

+----+---------+--------------------------------------------------------------------------------------------------+
| id | user_id | json_data                                                                                        |
+----+---------+--------------------------------------------------------------------------------------------------+
|  1 |       1 | {"url":"https://www.cpubenchmark.net/","title": "CPU Benchmarks"}                                |
|  2 |       1 | {"url":"http://www.ebay.com/sch/CPUs-Processors-/164/i.html","title": "Computer and Processors"} |
|  3 |       2 | {"url":"https://www.youtube.com/watch?v=tntOCGkgt98","title": "Funny Cats Compilation"           |
+----+---------+--------------------------------------------------------------------------------------------------+

我希望能够写出类似的东西:

   SELECT user_id, json_data FROM articles WHERE json_data.title LIKE "%CPU%"

这应该只返回第一行。

【问题讨论】:

标签: mysql json


【解决方案1】:

您可以使用json_extract(5.7 以上)。 https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#function_json-extract

SELECT user_id, json_data
FROM articles 
WHERE json_extract(json_data, '$.title') LIKE '%CPU%';

【讨论】:

  • 这是个好主意吗?用 json 查询?
【解决方案2】:

我最终以这种方式解决:https://github.com/ChrisCinelli/mysql_json 使用 UDF。我详细介绍了如何编译它并在 README 中安装它。它适用于我的 Ubuntu 12.04.5 gcc version 4.6.3MySQL 5.5

您将能够运行:

SELECT json_get('{"a":1}', 'a')       => 1
SELECT json_get('{"a":1}', 'b')       => NULL
SELECT json_get('[1,2,3]', 2)         => 3
SELECT json_get('{"a":[2]}', 'a', 0)  => 2

#Also:

SELECT json_get('{"a":{"b":2}}', 'a') => object
SELECT json_get('{"a":[1,2,3]}', 'a') => array

# Verify if it is a valid JSON:

SELECT ISNULL(json_get('{"a":1}'));   => 0  # Valid
SELECT ISNULL(json_get('{"a":1'));    => 1  # Invalid

# Create an example table:

CREATE TABLE `message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `data` text,
  PRIMARY KEY (`id`)
);
INSERT INTO message (id,data) VALUES(1,'{"from":"chris","title":"Awesome Article","body":"Lorem ipsum dolor sit amet, consectetur adipiscing elit."}');
INSERT INTO message (id,data) VALUES(2,'{"from":"loren","title":"Another Article","body":"Lorem ipsum dolor sit amet, consectetur adipiscing elit."}');
INSERT INTO message (id,data) VALUES(3,'{"from":"jason","title":"How to run a query","body":"Lorem ipsum dolor sit amet, consectetur adipiscing elit."}');

# Run queries on JSON values:

SELECT json_get(data,'title') FROM message WHERE id=2;
SELECT id,data FROM message WHERE json_get(data,'from')='chris';
SELECT id,data FROM message WHERE json_get(data,'title') LIKE '%Article%';

【讨论】:

  • 有人知道 Mariahdb 是否支持这个吗?
  • 我有 5.5.14 并且 FUNCTION json_get 不存在
  • @coding-dude.com json_get函数由链接的github页面中的UDF(用户定义函数)定义。它不是 MySQL 5.5 原生的。
【解决方案3】:

尝试以下查询,看看它是否符合您的需求:

SELECT user_id, json_data
FROM articles
WHERE common_schema.extract_json_value(json_data,'title')
LIKE "%CPU%"

这仅适用于MySQL 5.1 或更高版本。

【讨论】:

  • 我们使用 >= 5.5。我得到:FUNCTION common_schema.extract_json_value does not exist。我认为要使用我需要安装:code.google.com/p/common-schema - 我实际上开始寻找另一个插件。
【解决方案4】:

提取json数据的简单查询示例,

这将适用于 MySQL 和 MariaDB

1、JSON 对象

SELECT JSON_EXTRACT('{"a":1,"b":"stringdata"}','$.b');

输出:

"stringdata"

2、JSON对象多级

SELECT JSON_EXTRACT('{"a":1,"b":"stringdata","c":{"d":33}}','$.c.d') 

输出:

33

3、JSON数组

SELECT JSON_EXTRACT('[1,2,3,4]','$[0]')

输出:

1

4、JSON多级数组

SELECT JSON_EXTRACT('[1,2,3,[4,5]]','$[3][1]');

输出:

5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2015-04-20
    相关资源
    最近更新 更多