【问题标题】:How to get table data without mention columns name in row_to_json如何在 row_to_json 中不提及列名的情况下获取表数据
【发布时间】:2020-11-11 23:15:07
【问题描述】:

下面的脚本非常适合指定的列,我得到了我期望的结果。但我想从表中获取所有列。谢谢你的帮助。

SELECT row_to_json(fc) FROM (
 SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features FROM(
  SELECT 'Feature' As type, ST_AsGeoJSON(lg.geom)::json As geometry,
      row_to_json((id, name)) As properties FROM coffee_shops As lg
    ) As f
) As fc

【问题讨论】:

  • 你好。您还可以添加表结构、一些数据样本和确切的预期结果吗?
  • "type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":1,"name":"1369 Coffee House" ,"address":"1369 Cambridge St","city":"Cambridge","state":"MA","zip":"02139"},"geometry":{"type":"Point","坐标":[-71.10044,42.373695]}} 预期结果
  • 请添加您的表结构以获得精确的 quary

标签: javascript sql node.js postgresql postgis


【解决方案1】:

要包含所有字段(包括几何),您可以将表名或别名传递给 row_to_json

select 
row_to_json(lg) As properties FROM coffee_shops As lg

【讨论】:

  • 但是geom文件不能在geojson中转换
  • 如果您将 all 字段放入 json 中,则它必须包含几何图形,此代码将正确转换它。有些读者会很好地使用它。是否要排除某些字段由您决定...但您的问题指定您想要所有字段
  • 对不起,我提到的,“type”:“FeatureCollection”,“features”:[{“type”:“Feature”,“properties”:{“id”:1,”name ":"1369 Coffee House","address":"1369 Cambridge St","city":"Cambridge","state":"MA","zip":"02139"},"geometry":{"type ":"Point","coordinates":[-71.10044,42.373695]}} 预期结果
猜你喜欢
  • 1970-01-01
  • 2019-09-26
  • 2014-05-22
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 2011-03-29
相关资源
最近更新 更多