我正在尝试从数据库中以JSON格式查询数据:
+---------room table-------+
| shapeid | geom | surface |
+--------------------------+
| 1 | xxxxx| 50m2 |
| 2 | xxxxx| 25m2 |
+-------users table---------+
| userid | name | position |
+---------------------------+
| 1 | fred | teacher |
| 2 | timmy | student |
+----------roomusers table-------+
| roomuserid | shapeid | userid |
+--------------------------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
我通过以下查询设法获得了桌子上的所有房间:
SELECT jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(feature)
)
FROM (
SELECT jsonb_build_object(
'type', 'Feature',
'id', shapeid,
'geometry', ST_AsGeoJSON(ST_TRANSFORM(geom,4326))::json,
'properties', to_jsonb(row)
) AS feature
FROM (SELECT * FROM rooms) row) features;
其结果如下:
{
"type":"FeatureCollection",
"features":[
{
"id":1,
"type":"Feature",
"geometry":{
"type":"MultiPolygon",
"coordinates":[
"xxxxxx"
]
},
"properties":{
"geom":"xxxxx",
"shapeid":1,
"surface":"50m2"
}
}
}
但是,我也想为每个功能都提供一个额外的数组,其中包含每个房间用户的数据,而Im在这里完全丢失了。