尝试将3个子表折叠到父列的JSON列中时,出现“错误代码:1111.组功能的无效使用”。
我知道这是违反“一切正常化”规则的主要罪过,但这恰恰是我所需要的。
我有4张桌子:
CREATE TABLE `requirement_categories` (
`requirement_category` varchar(128) NOT NULL,
PRIMARY KEY (`requirement_category`),
UNIQUE KEY `category_UNIQUE` (`requirement_category`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `requirement_groups` (
`requirement_group` varchar(128) NOT NULL,
`requirement_category` varchar(128) DEFAULT NULL,
PRIMARY KEY (`requirement_group`),
KEY `fk_requirement_caregory_idx` (`requirement_category`),
CONSTRAINT `fk_requirement_caregory` FOREIGN KEY
(`requirement_category`) REFERENCES `requirement_categories`
(`requirement_category`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `requirement_subgroups` (
`group_id` varchar(64) DEFAULT NULL,
`requirement_subgroup` varchar(128) NOT NULL,
`description` text,
`requirement_group` varchar(128) DEFAULT NULL,
PRIMARY KEY (`requirement_subgroup`),
KEY `fk_requirement_group_idx` (`requirement_group`),
CONSTRAINT `fk_requirement_group` FOREIGN KEY
(`requirement_group`) REFERENCES `requirement_groups`
(`requirement_group`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `requirements` (
`requirement` varchar(256) NOT NULL,
`description` text,
`requirements_subgroup` varchar(128) NOT NULL,
PRIMARY KEY (`requirement`,`requirements_subgroup`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我的查询如下:
SELECT
rc.requirement_category,
JSON_OBJECT('requirement_group',
rg.requirement_group,
'requirement_subgroup',
JSON_ARRAYAGG(JSON_OBJECT('requirement_subgroup',
rsg.requirement_subgroup,
'description',
rsg.description,
'requirements',
JSON_ARRAYAGG(JSON_OBJECT('requirement',
rs.requirement,
'description',
rs.description))))) AS
requirement
FROM
jobdesc_jdc.requirement_categories rc
LEFT JOIN
requirement_groups rg ON rg.requirement_category =
rc.requirement_category
LEFT JOIN
requirement_subgroups rsg ON rsg.requirement_group =
rg.requirement_group
LEFT JOIN
requirements rs ON rs.requirements_subgroup =
rsg.requirement_subgroup
GROUP BY rg.requirement_category
以增量方式执行此操作,直到我添加最后一个JSON_OBJECT函数(在查询中首先出现)为止,一切正常,然后可以进入结果集,该结果集的第一列中的类别为第二个ana JSON对象中的组总结其余部分。我只需要两列,第一列为类别,第二列为JSON对象。
我将不胜感激-我已经搜寻了很多东西,并花了数小时的时间。
此致
会。
答案 0 :(得分:1)
通过运气和绝望,我成功了。我封闭了有效的阶段,将子表和孙表合并到JSON列,视图中,然后将其用作最终父级的子级。
SELECT
requirement_category,
JSON_ARRAYAGG(JSON_OBJECT(requirement_group, requirement)) as requirements
FROM
(SELECT
rc.requirement_category,
rg.requirement_group,
JSON_OBJECT(
'requirement_subgroup', rsg.requirement_subgroup,
'description', rsg.description, 'requirements',
JSON_ARRAYAGG(
JSON_OBJECT(
'requirement', rs.requirement,
'description', rs.description
)
)
) AS requirement
FROM jobdesc_jdc.requirement_categories rc
JOIN requirement_groups rg ON rg.requirement_category = rc.requirement_category
JOIN requirement_subgroups rsg ON rsg.requirement_group = rg.requirement_group
JOIN requirements rs ON rs.requirements_subgroup = rsg.requirement_subgroup
GROUP BY rc.requirement_category , rg.requirement_group
) v1
GROUP BY requirement_category