这是这个优秀问答的后续:13227142。
我几乎必须做同样的事情(在 PostgreSQL 9.2 的约束下),但我只使用一张表。因此,查询使用了自连接(为了生成正确的 JSON 格式),这会导致重复的id字段。我怎样才能避免这种情况?
例子:
CREATE TABLE books
(
id serial primary key,
isbn text,
author text,
title text,
edition text,
teaser text
);
SELECT row_to_json(row)
FROM
(
SELECT id AS bookid,
author,
cover
FROM books
INNER JOIN
(
SELECT id, title, edition, teaser
FROM books
) cover(id, title, edition, teaser)
USING (id)
) row;
结果:
{
"bookid": 1,
"author": "Bjarne Stroustrup",
"cover": {
"id": 1,
"title": "Design and Evolution of C++",
"edition": "1st edition",
"teaser": "This book focuses on the principles, processes and decisions made during the development of the C++ programming language"
}
}
我想摆脱“封面”中的“id”。