微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pgsql 合并两个 json 数组

如何解决pgsql 合并两个 json 数组

我必须从表中选择两种类型的结果集并将它们合并到单个 json 数组中 喜欢

select col1,col2,col3 from table 1 order by col3 where col1< 2
union all 
select col1,col3 from table 1 order by col1 where colo = 3

我认为这是由于 order by 导致的语法错误

我需要在 json 数组中返回它,所以我正在尝试这样

SELECT (    SELECT json_agg(row)
            FROM (
         select col1,col3 from table 1 order by col3 where col1< 2 ) row)
UNION ALL
  SELECT (SELECT json_agg(row)
            FROM (
        select col1,col3 from table 1 order by col1 where colo = 3
) row)

它正在停用两个 json 数组,即很明显,我怎样才能在单个数组中做到这一点

谢谢,

解决方法

在 UNION 之后进行聚合:

select json_agg(t)
from (
  (select col1,col2,col3 from table1 order by col3 where col1< 2)
  union all 
  (select col1,col3 from table1 order by col1 where colo = 3)
) t

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。