使用此查询获取json结果,如下面的数据结构,
SELECT c.* ,
row_to_json(mg.*) as media_gallery,
row_to_json(m.*) as media,
row_to_json(g.*) as gallery
FROM comment c
LEFT JOIN media m ON m.id = c.endpoint_id
.... join mg g table
是否可以根据列值选择选择表可能就像这样的语法,如果为null则排除结果中的media_gallery,media和gallery
SELECT c.* ,
CASE WHEN c.endpoint_code = 1
THEN
row_to_json(mg.*) as media_gallery,
row_to_json(m.*) as media,
END
CASE WHEN c.endpoint_code = 0 THEN
row_to_json(g.*) as gallery
END
FROM comment c
LEFT JOIN media m ON m.id = c.endpoint_id
.... join mg g table
结果
rows:
[ { id: 7,
endpoint_code: 0,
endpoint_id: 27,
status: 0,
media_gallery: null,
media: null,
gallery:
{ id: 27,
status: 0,
create_date:
...
更新
我也尝试了这个,但没有工作
COALESCE( row_to_json(mg.*), null) AS media_gallery,
答案 0 :(得分:0)
这是你想要的吗?
SELECT c.* ,
(CASE WHEN c.endpoint_code = 1 THEN row_to_json(mg.*) END) as media_gallery,
(CASE WHEN c.endpoint_code = 1 THEN row_to_json(m.*) END) as media,
(CASE WHEN c.endpoint_code = 0 THEN row_to_json(g.*) END) as gallery
. . .