我的查询是
select array_to_json(array_agg(row_to_json(t))) from (
select count(*) over() as total, id,title
FROM notifications
WHERE title ilike '%a%'
LIMIT 5 OFFSET 1)t
,输出
[
{
"title": "Sartaj ",
"id": 3,
"total":16
}
]
结果包括对象中的总数,但我想将它移到对象之外。
我的预期答案是
[
"total":16,
"data":[
{
"title": "Sartaj ",
"id": 3
}]
]
我也试过
with tmp_notifications as (
SELECT id,title,description,img_url,link,created_on from notifications
where title ilike '%universi%'
)
select row_to_json(t) from (
select count(*) as data_count,
(json_agg(json_build_object('description',description,'title',title,'created_on',created_on,
'link',link,'img_url',img_url,'id',id))
) as data from tmp_notifications limit 1
)t
但限制在这里不起作用。
答案 0 :(得分:1)
使用json_build_object()
和json_agg()
:
private static class LazyResourceHolder {
public static Resource resource = new Resource();
}
...
public static Resource getInstance() {
return LazyResourceHolder.something;
}
请参阅DbFiddle.
中的完整示例您可以使用select json_build_object('total', count(*), 'data', json_agg(to_json(t)))
from (
select id, title
from notifications
where title ilike '%a%'
) t
语句计算所有已过滤的行,但会显示其数量有限,例如:
WITH