web-dev-qa-db-fra.com

Comment écrire dans MySQL des requêtes pouvant analyser des données JSON dans une colonne?

J'ai une table dans MySQL qui a une colonne qui stocke des objets JSON. Comment puis-je exécuter facilement des requêtes pouvant contenir des champs JSON dans la clause WHERE?

EX: Avec une table nommée 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"           |
+----+---------+--------------------------------------------------------------------------------------------------+

Je veux être capable d'écrire quelque chose comme:

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

Cela devrait renvoyer que la première ligne.

13
Chris Cinelli

J'ai fini par résoudre de cette manière: https://github.com/ChrisCinelli/mysql_json avec une UDF. J'ai détaillé comment le compiler et l'installer dans le fichier README. Cela fonctionne pour moi sur Ubuntu 12.04.5 sur gcc version 4.6.3 avec MySQL 5.5

Vous pourrez courir:

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%';
4
Chris Cinelli

Vous pouvez utiliser json_extract. 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%';
2
northtree

Essayez la requête suivante et voyez si elle répond à vos besoins:

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

Cela ne fonctionnera que sur MySQL version 5.1 ou plus récente.

1
Tim Biegeleisen