열의 JSON 데이터를 구문 분석할 수 있는 쿼리를 MySQL에서 어떻게 쓸 수 있습니까?
MySQL에 JSON 개체를 저장하는 열이 있는 테이블이 있습니다.WHERE 절에 JSON 필드 중 일부를 포함할 수 있는 쿼리를 쉽게 실행하려면 어떻게 해야 합니까?
EX: 이름 있는 테이블과 함께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%"
그러면 첫 번째 행만 반환됩니다.
사용할 수 있습니다.json_extract
(5.7 Up).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%';
이 방법으로 해결했습니다.https://github.com/ChrisCinelli/mysql_json with UDF. 컴파일하여 README에 설치하는 방법에 대해 자세히 설명했습니다.이 기능은Ubuntu 12.04.5
에gcc version 4.6.3
와 함께MySQL 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%';
json 데이터를 추출하는 간단한 쿼리 예제입니다.
MySQL 및 Maria에서 동작합니다.DB
1, JSON 오브젝트
SELECT JSON_EXTRACT('{"a":1,"b":"stringdata"}','$.b');
출력:
"stringdata"
2, JSON Objct 멀티 레벨
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
다음 쿼리를 시도하여 요구에 적합한지 확인하십시오.
SELECT user_id, json_data
FROM articles
WHERE common_schema.extract_json_value(json_data,'title')
LIKE "%CPU%"
이것은 다음에서만 유효합니다.MySQL
버전 5.1 이후
json_extract(설명, '$contacts') AS 연락처, json_extract(설명, '$name') AS 이름 FROM을 선택합니다.orders
위치 1
언급URL : https://stackoverflow.com/questions/29137915/how-can-write-queries-in-mysql-that-can-parse-json-data-in-a-column
'programing' 카테고리의 다른 글
MySQL에서 SLEEP()를 올바르게 사용하는 방법 및 시기 (0) | 2022.12.10 |
---|---|
끈에 0을 어떻게 채워요? (0) | 2022.12.10 |
STRAY_JOIN이 이 쿼리를 대폭 개선하는 이유는 무엇이며 SELECT 키워드 뒤에 쓰여지는 것은 무엇을 의미합니까? (0) | 2022.12.10 |
Panda 데이터 프레임에서 열 수를 검색하려면 어떻게 해야 합니까? (0) | 2022.12.10 |
MySQL과 MariaDB 데이터베이스의 차이점은 무엇입니까? (0) | 2022.12.10 |