我可以使用group_concat内另一个group_concat在SQL?



所以我有一个这样的查询:

select
p.identifier,
GROUP_CONCAT(
'[' ||
'{"thumbnail":' ||
'"' ||
ifnull(s.thumbnail,'null') ||
'"' ||
',"title:' ||
'"' ||
s.title ||
'","content": [' ||
GROUP_CONCAT(
'{"text":' ||
ifnull(c.text,'null') ||
'", "image":' || 
ifnull(c.image,'null') ||
'", "caption": "' ||
ifnull(c.caption,'null') ||
'"},'
) ||
']},'
)
from pois as p
join  stories as s on p.identifier = s.poiid
join content c on s.storyid = c.storyid
group by s.storyid 

And i got And error:

在准备中,聚合函数GROUP_CONCAT()的误用

要清楚地看到我有一个名为POIS的大对象,每个POI都有多个故事,每个故事都有多个内容,我想显示x行(我有多少POIS),列内有每个故事连接到他们的POI(以及故事中的每个内容),我需要json格式,这样我就可以解析数据库查询并读回我的对象。

我希望清楚我的问题是什么,你可以帮助我。

所以我把查询改成这样:

SELECT p.identifier, (
SELECT json_group_array(json_object('id', s.storyid))
FROM stories AS s
WHERE s.poiid=p.identifier
) as stories,
(
SELECT json_group_array(json_object('id', c.contentid, 'storyId', s.storyid))
FROM content AS c
JOIN stories AS s ON c.storyid=s.storyid
WHERE s.poiid=p.identifier
) as contents
FROM pois AS p
GROUP BY p.identifier

这是我的结果:输入图片描述

但是我想把第三列放在第二列里面(每个点都有多个故事,每个故事都有一个或多个内容,所以内容应该在他们的故事里面。

最新更新