考虑MySQL 5.7.x中有一个JSON列的表。
-- CREATE TABLE "plans" -----------------------------------
CREATE TABLE `plans` (
`id` VarChar( 36 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL,
`name` VarChar( 50 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`structure` JSON NOT NULL,
PRIMARY KEY ( `id` ),
CONSTRAINT `index_exam_plans_on_id` UNIQUE( `id` ),
CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci
ENGINE = InnoDB;
structure
列有一个JSON。请在下面找到JSON的示例结构:
{
"33aa1e1c-0c95-4860-9b71-ccd13f393dd0": {
"name": "Term 1",
"tags": [
"Term"
],
"type": "default",
"uuid": "33aa1e1c-0c95-4860-9b71-ccd13f393dd0",
"is_locked": false
},
"cb896a12-f07c-4bcc-9c22-7bdfa585f5f7": {
"name": "English",
"tags": [
"Paper",
"Course Paper"
],
"type": "course_paper",
"uuid": "cb896a12-f07c-4bcc-9c22-7bdfa585f5f7",
"course_id": 1,
"is_locked": false
},
"e6d2f9fb-0429-42b2-b704-c438e1695044": {
"name": "Written Work",
"tags": [
"Paper",
"Regular Paper"
],
"type": "regular_paper",
"uuid": "e6d2f9fb-0429-42b2-b704-c438e1695044",
"course_id": 2,
"is_locked": false
},
"d0d3eeff-9ffb-4f35-b0fb-b94373d1fe5b": {
"name": "Summative Assessment",
"tags": [
"Exam"
],
"type": "default",
"uuid": "d0d3eeff-9ffb-4f35-b0fb-b94373d1fe5b",
"is_locked": false
},
"0952e100-cd4a-473e-bd24-2370e0dfcc1c": {
"name": "Speaking skills",
"tags": [
"Paper",
"Regular Paper"
],
"type": "regular_paper",
"uuid": "0952e100-cd4a-473e-bd24-2370e0dfcc1c",
"course_id": 5,
"is_locked": false
}
}
注意事项:
type
的密钥。default
作为type
键的值。我想找到type
设置为default
的所有内部JSON。这应该只在SQL中完成。对于上面的示例,输出应该只是第一个和第四个内部JSON。
我该怎么做?
我尝试过以下操作,但它不会根据条件返回内部JSON。它返回structure
列中的每个内部JSON。
select JSON_EXTRACT(structure, '$.*') from plans
where name = 'Academic'
and JSON_CONTAINS( structure->'$.*.type', '"default"' );
有人能给出正确的解决方法吗?