我有一个与Invalid use of group function
MySQL 相关的问题。
我要执行的查询是这样的(我知道这是一个非常大的查询,但如果我能执行这个,那么我将调整和修复性能和易于阅读的内容):
SELECT
`id_c` AS `sceneId`,
`scene`.`id` AS `sceneName`,
`scene`.`frameCount` AS `frames`,
task.id_mv AS `id_mv`,
CONCAT( '[',GROUP_CONCAT(
CONCAT(
'{',
'"idTk": ', `task`.`id_tk`, ', ',
-- '"tktype": ', `task`.`id_tk_type`, ', ',
-- '"subDepType": ', IF ( `task`.`id_sub_dep_type` = 54, `task`.`id_sub_dep_type`, 'null'), ', ',
-- '"color": "', `task`.`color`, '", ',
'"weight": ', MAX(`task`.`weight`), ', ',
'"dept": ', '"',
CASE `task`.`id_tk_type`
WHEN 43 THEN 'MVB'
WHEN 12 THEN 'LYT'
WHEN 32 THEN
CONCAT_WS(
', ',
IF( `task`.`id_sub_dep_type` != 54, 'ANI', NULL ),
IF( `task`.`id_sub_dep_type` = 54, 'CKD', NULL )
)
WHEN 190 THEN 'DYN'
WHEN 104 THEN 'FIN'
WHEN 191 THEN 'LGT'
END, '"',
-- task.id_tk_type, ', ',
-- task.id_sub_dep_type,
'}'
)
ORDER BY
FIELD( `task`.`id_tk_type`, 43, 12, 32, 190, 104, 191 ),
`task`.`weight` DESC
SEPARATOR
', '
), ']') AS `tkInfo`
FROM
`V_task` AS `task`
JOIN `tk_fct_mcsc` AS `mcsc` ON `task`.`id_tk` = `mcsc`.`id_tk`
JOIN `scene` ON `mcsc`.`id_c` = `scene`.`id_scene`
WHERE
`task`.`id_sub_dep_type` IN (
SELECT
`id_sub_dep_type`
FROM
`tk_dim_sub_dep_type`
WHERE `id_tk_type` IN (
SELECT
`id_tk_type`
FROM
`tk_dim_dep_type`
WHERE
`id_tk_type` IN ( 43, 12, 32, 190, 104, 191 )
)
AND `id_sub_dep_type` != 54
UNION
SELECT
`id_sub_dep_type`
FROM
`tk_dim_sub_dep_type`
WHERE
`id_sub_dep_type` = 54
)
AND `task`.`is_appr` = FALSE
AND `mcsc`.`type` = 'sc'
AND `scene`.`id` != '000'
-- AND `mcsc`.`id_b` = 530
GROUP BY
`scene`.`id`,
`scene`.`id_scene`
-- task.weight
ORDER BY
`scene`.`id`,
`task`.`weight` DESC;
错误,我知道是函数MAX( task.weight )
内部GROUP_CONCAT
,但我不知道如何避免这件事..
我实际上正在尝试构建某种 sqlfiddle 东西来让您测试这些东西,但我认为我需要的只是另一双让我知道去哪里看的眼睛。
谢谢!
编辑#1 正如kickstart所问,我想要获得的是这样的:
sceneId sceneName frames id_mv tkInfo
200 001 1200 1 "[{"dept": "LGT"}, {"dept": "LGT"}]"
1342 001 45 14 "[{"dept": "LYT"}, {"dept": "LGT"}, {"dept": "LGT"}]"
1335 001 460 14 "[{"dept": "MVB"}, {"dept": "LYT"}, {"dept": "LGT"}, {"dept": "LGT"}]"
1351 001 20 11 "[{"dept": "LYT"}, {"dept": "ANI"}, {"dept": "FIN"}, {"dept": "LGT"}]"
1375 001 63 11 "[{"dept": "LYT"}, {"dept": "CKD"}, {"dept": "ANI"}, {"dept": "DYN"}, {"dept": "FIN"}, {"dept": "FIN"}, {"dept": "FIN"}, {"dept": "LGT"}]"
1382 001 66 11 "[{"dept": "LYT"}, {"dept": "CKD"}, {"dept": "ANI"}, {"dept": "FIN"}]"
但是,如果没有“部门”的重复,并且这样做,我需要那个部门的最大重量。
编辑#2 我用一点作弊解决了我的这个问题。
首先,我简化了很多主要查询,删除了case-thing并为每个sceneId插入了一个MAX(weight)。
通过这个简化的查询,我构建了一个视图,并通过该视图应用了 case/group_concat 的东西..所有结果都很好。
[ ..sqlfiddle 将立即插入此处.. ]