如何选择这种格式的书:
{
title: 'Avengers',
description:'Good book',
tags: [{id:1, name: 'Drama'},{id:2, name: 'Horror'}]
authors: [{id:1, name: 'Alex'},{id:2, name: 'Tanya'}]
}
对于具有多对多关系的表:
或者提出3个不同的pool.request是更好的选择吗?
书
|id|title |description|
|1 |'Avengers'|'Good book'|
|2 |'Fear' |'Scary'
作者
|id|name
|1 |'Alex'
|2 |'Tanya'
authors_books
|book_id|author_id
|1 |1
|1 |2
|2 |1
标签
|id|name
|1 |'Drama'
|2 |'Horror'
tags_books
|book_id|tag_id
|1 |1
|1 |2
|2 |1
我以前没有作者表时就使用过这个
SELECT b.title, b.id, b.description,
json_agg(json_build_object('id', t.id, 'name', t.name, 'description',
t.description, 'likes', tb.likes) ORDER BY tb.likes DESC) AS tags
FROM books AS b
INNER JOIN tags_books AS tb ON (b.id = tb.book_id)
INNER JOIN tags AS t ON (tb.tag_id = t.id)
WHERE b.id = $1
GROUP BY b.id;`
但是对于表自动操作器,使用新的INNER JOIN将有重复的值。
答案 0 :(得分:1)
您应将数据(authors
和tags
)聚集在单独的派生表(FROM
子句中的子查询)中:
select
b.title,
b.description,
t.tags,
a.authors
from books as b
join (
select
tb.book_id,
json_agg(json_build_object('id', t.id, 'name', t.name) order by tb.likes desc) as tags
from tags_books as tb
join tags as t on tb.tag_id = t.id
group by tb.book_id
) t on b.id = t.book_id
join (
select
ab.book_id,
json_agg(json_build_object('id', a.id, 'name', a.name) order by ab.author_id) as authors
from authors_books as ab
join authors as a on ab.author_id = a.id
group by ab.book_id
) a on b.id = a.book_id
title | description | tags | authors
----------+-------------+---------------------------------------------------------------+-------------------------------------------------------------
Avengers | Good book | [{"id" : 2, "name" : "Horror"}, {"id" : 1, "name" : "Drama"}] | [{"id" : 1, "name" : "Alex"}, {"id" : 2, "name" : "Tanya"}]
Fear | Scary | [{"id" : 1, "name" : "Drama"}] | [{"id" : 1, "name" : "Alex"}]
(2 rows)