【问题标题】:I want to get part of a data from a postgresql database as a json object我想从 postgresql 数据库中获取部分数据作为 json 对象
【发布时间】:2020-10-02 05:17:40
【问题描述】:

我有一个从中获取数据的 postgres 数据库。但是,我正在合并 2 个表中的数据,并且我希望将另一个表中的数据作为名为车辆详细信息的 Json 对象返回。如果您能纠正我弄错 json_build_object 的地方,我将不胜感激。我现在还不清楚 json_build_object 是如何工作的

查询如下

SELECT      vr.id, users.first_name, users.last_name,
        mechanic_signature, supervisor_signature
        FROM vr 
        LEFT JOIN users ON vr.inspector_id=users.id 
        LEFT JOIN ( SELECT id,
        json_build_object( 
        'id', veh.id,
        'make', veh.make,
        ) AS vd
        FROM vehicles v
        ) vr
        ON vr.vehicle_id = v.id
        WHERE vr.id=1
        GROUP BY  vr.id, users.first_name, users.last_name,
        mechanic_signature, supervisor_signature

【问题讨论】:

    标签: sql json database postgresql oop


    【解决方案1】:

    json_build_object 的格式正确:

    select json_build_object('cell_per', cell_per, 'category', category) from cell_per;
                  json_build_object               
    ----------------------------------------------
     {"cell_per" : 18, "category" : "HERB 3.5"}
     {"cell_per" : 18, "category" : "H PREM 3.5"}
     {"cell_per" : 1, "category" : "HERB G"}
     {"cell_per" : 1, "category" : "HERB TOP"}
    

    你遇到了什么错误?

    【讨论】:

    • 谢谢阿德里安,我能弄明白
    猜你喜欢
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 2014-06-10
    • 2012-08-24
    相关资源
    最近更新 更多