我正在使用PostgreSQL 9.3中提供的row_to_json
函数来获取JSON的查询结果:
SELECT row_to_json(_customer_wishes) FROM (
SELECT
...
(SELECT row_to_json(_brand)
FROM (
SELECT b.id, b.name, b.url
) AS _brand
) AS brand,
JOIN brand AS b ON ...
WHERE ...
) AS _customer_wishes;
但我不喜欢(SELECT row_to_json(_brand) FROM (SELECT b.*) AS _brand ) AS brand
。
我想要(SELECT row_to_json(SELECT b.*)) AS brand
之类的东西,但我不确定它是否可行。
答案 0 :(得分:0)
通常我会在这种情况下使用类型(我们以面向对象的方式使用数据库)。
仅举例:
CREATE TYPE type_brand AS
(
id integer,
name text,
url text
);
你的子查询:
SELECT row_to_json(SELECT (b.id, b.name, b.url)::type_brand) AS brand.
但是,对于表结构和desidered结果,我认为我可以建议一个更优雅的查询,你可以只使用row_to_json一次。