我正在从各种表中构建一个 JSON 对象。我已经成功获得以下代码几乎可以做我想做的事。唯一的问题是聚合数组被设置为另一个对象,我只希望主对象中的项目数据指向所选行的数组。
SELECT jsonb_build_object('ok',false,'data',f1) FROM
(SELECT array_agg(f2) FROM (
SELECT c.catalogid,cl.title,cl.shortname FROM
shopmaster.catalog c LEFT JOIN
shopmaster.catalog_lang cl ON c.catalogid=cl.catalogid LEFT JOIN
shopmaster.lang l ON cl.langid=l.langid
WHERE inherit_from=3 AND code='en') f2) f1;
这将返回以下 json
{"ok": false, "data": {"array_agg": [{"title": "Vitamins", "catalogid": 6, "shortname": "vit"}, {"title": "Cheese Vitamins", "catalogid": 7, "shortname": "chevit"}, {"title": "fruitveg", "catalogid": 8, "shortname": "fruit and veg"}, {"title": "Magazines", (...)
当我想要
{"ok": false, "data": [{"title": "Vitamins", "catalogid": 6, "shortname": "vit"}, {"title": "Cheese Vitamins", "catalogid": 7, "shortname": "chevit"}, {"title": "fruitveg", "catalogid": 8, "shortname": "fruit and veg"}, {"title": "Magazines", (...)