如何将多个一对多结果与Postgres json_agg结合使用?

时间:2018-04-16 22:49:41

标签: sql json postgresql aggregate-functions

函数

中的多个json_aggregates

我的Postgres数据库的主表与其他表有一对一对多的关系,每个表都由[*]_references表连接。我需要一个结果,返回其他表的结果作为JSON。我可以联合起来获取我想要的数据:

SELECT ft.id, json_agg(genres) AS genres 
FROM ft_references ft 
INNER JOIN genre_references gr ON gr.reference_id = ft.id 
INNER JOIN genres_catalog genres ON gr.genre_id = genres.id 
WHERE ft.id = 2 GROUP BY ft.id;

 id |                      genres                      
----+--------------------------------------------------
  2 | [{"id":1,"name":"Action","other_info":null},    +
    |  {"id":2,"name":"Adventure","other_info":null}, +
    |  {"id":5,"name":"Comedy","other_info":null},    +
    |  {"id":8,"name":"Drama","other_info":null},     +
    |  {"id":10,"name":"Fantasy","other_info":null},  +
    |  {"id":13,"name":"Horror","other_info":null},   +
    |  {"id":25,"name":"War","other_info":null}]
(1 row)

同样是我的第二组结果:

SELECT ft.id, json_agg(tales) AS tales 
FROM ft_references ft 
INNER JOIN tale_references tr ON tr.reference_id = ft.id 
INNER JOIN tale_catalog tales ON tales.id = tr.tale_catalog_id 
WHERE ft.id = 2 GROUP BY ft.id;

 id |                                      tales                                      
----+---------------------------------------------------------------------------------
  2 | [{"id":8,"tale_id":"124","tale_type":"The Three Brothers","other_info":null},  +
    |  {"id":39,"tale_id":"327A","tale_type":"Hansel and Gretel","other_info":null}, +
    |  {"id":82,"tale_id":"570","tale_type":"Pied Piper","other_info":null}]
(1 row)

这两个结果都是正确的。然而,当我想把它们组合在一起时,我突然得到了我的类型和故事结果的产物(即:三重结果!):

SELECT ft.id, json_agg(genres) AS genres, json_agg(tale) AS tales 
FROM ft_references ft 
INNER JOIN genre_references gr ON gr.reference_id = ft.id 
INNER JOIN genres_catalog genres ON gr.genre_id = genres.id 
INNER JOIN tale_references tr ON tr.reference_id = ft.id 
INNER JOIN tale_catalog tale ON tale.id = tr.tale_catalog_id 
WHERE ft.id = 2 GROUP BY ft.id, gr.reference_id, tr.reference_id;
 id |                      genres                      |                                      tales                                      
----+--------------------------------------------------+---------------------------------------------------------------------------------
  2 | [{"id":1,"name":"Action","other_info":null},    +| [{"id":82,"tale_id":"570","tale_type":"Pied Piper","other_info":null},         +
    |  {"id":1,"name":"Action","other_info":null},    +|  {"id":39,"tale_id":"327A","tale_type":"Hansel and Gretel","other_info":null}, +
    |  {"id":1,"name":"Action","other_info":null},    +|  {"id":8,"tale_id":"124","tale_type":"The Three Brothers","other_info":null},  +
    |  {"id":2,"name":"Adventure","other_info":null}, +|  {"id":82,"tale_id":"570","tale_type":"Pied Piper","other_info":null},         +
    |  {"id":2,"name":"Adventure","other_info":null}, +|  {"id":39,"tale_id":"327A","tale_type":"Hansel and Gretel","other_info":null}, +
    |  {"id":2,"name":"Adventure","other_info":null}, +|  {"id":8,"tale_id":"124","tale_type":"The Three Brothers","other_info":null},  +
    |  {"id":5,"name":"Comedy","other_info":null},    +|  {"id":82,"tale_id":"570","tale_type":"Pied Piper","other_info":null},         +
    |  {"id":5,"name":"Comedy","other_info":null},    +|  {"id":39,"tale_id":"327A","tale_type":"Hansel and Gretel","other_info":null}, +
    |  {"id":5,"name":"Comedy","other_info":null},    +|  {"id":8,"tale_id":"124","tale_type":"The Three Brothers","other_info":null},  +
    |  {"id":8,"name":"Drama","other_info":null},     +|  {"id":82,"tale_id":"570","tale_type":"Pied Piper","other_info":null},         +
    |  {"id":8,"name":"Drama","other_info":null},     +|  {"id":39,"tale_id":"327A","tale_type":"Hansel and Gretel","other_info":null}, +
    |  {"id":8,"name":"Drama","other_info":null},     +|  {"id":8,"tale_id":"124","tale_type":"The Three Brothers","other_info":null},  +
    |  {"id":10,"name":"Fantasy","other_info":null},  +|  {"id":82,"tale_id":"570","tale_type":"Pied Piper","other_info":null},         +
    |  {"id":10,"name":"Fantasy","other_info":null},  +|  {"id":39,"tale_id":"327A","tale_type":"Hansel and Gretel","other_info":null}, +
    |  {"id":10,"name":"Fantasy","other_info":null},  +|  {"id":8,"tale_id":"124","tale_type":"The Three Brothers","other_info":null},  +
    |  {"id":13,"name":"Horror","other_info":null},   +|  {"id":82,"tale_id":"570","tale_type":"Pied Piper","other_info":null},         +
    |  {"id":13,"name":"Horror","other_info":null},   +|  {"id":39,"tale_id":"327A","tale_type":"Hansel and Gretel","other_info":null}, +
    |  {"id":13,"name":"Horror","other_info":null},   +|  {"id":8,"tale_id":"124","tale_type":"The Three Brothers","other_info":null},  +
    |  {"id":25,"name":"War","other_info":null},      +|  {"id":82,"tale_id":"570","tale_type":"Pied Piper","other_info":null},         +
    |  {"id":25,"name":"War","other_info":null},      +|  {"id":39,"tale_id":"327A","tale_type":"Hansel and Gretel","other_info":null}, +
    |  {"id":25,"name":"War","other_info":null}]       |  {"id":8,"tale_id":"124","tale_type":"The Three Brothers","other_info":null}]
(1 row)

我如何重构我的查询,以便我只得到第一个例子中的正确`genres`结果,以及第二个例子中正确的`tales`结果,而不是过度完成的事情在我的实际(第三)例子中?

1 个答案:

答案 0 :(得分:1)

答案如下:

test = '123456789123'
result = ''

while test:
    result += test[:3]
    if len(test) > 3:
        result += '.'
    test = test[3:]

print(result)