Postgresql 或GreenPlum 查询结果部分字段转json格式并保留字段名(row_to_json)


-- 一些搜索结果给出 部分字段转json保留原字段的方式是用子查询
select
row_to_json(t) from ( select id, text from words ) t

但是如果子查询 有where条件会导致结果又为{"f1":1,"f2":2,"f3":"foo"}这种格式,比较不便。

【解决方法】

在子查询最后加上limit 99999999999(数字大于查询结果数量即可)

select row_to_json(t)
from (
  select id, text from words where text like '%abc%' limit 999999999
) t