我有这样一个问题:
SELECT tags
FROM (
SELECT trm.dado_id
, json_agg(json_build_object('field',trm.nome, 'value',trm.valor)) AS tags
FROM tb_dados trm
JOIN tb_table1 t on trm.dado_id = t.id
WHERE t.id = trm.dado_id
GROUP BY trm.dado_id
) tabletags;
它返回了很多带有json数组字段的行,如下所示:
1 | [{"field" : "EMISSION", "value" : "21/04/2020 00:38:00"}, {"field" : "DATA CREATION", "value" : "21/09/1989"}, {"field" : "SERIE NUMBER", "value" : "00.000.000-11"}]
2 | [{"field" : "DATA CREATION", "value" : "21/09/1998"}, {"field" : "SERIE NUMBER", "value" : "00.000.000-7"}]
3 | [{"field" : "EMISSION", "value" : "21/04/2020 00:38:00"}, {"field" : "DATA CREATION", "value" : "21/09/1989"}, {"field" : "SERIE NUMBER", "value" : "00.000.000-7"}]
4 | [{"field" : "EMISSION", "value" : "21/04/2020 00:38:00"}, {"field" : "DATA CREATION", "value" : "21/09/1989"}, {"field" : "SERIE NUMBER", "value" : "00.000.000-11"}]
...
我想查询序列号为00.000.000-11的行。有办法过滤吗?
1条答案
按热度按时间mklgxw1f1#
可以使用
HAVING
条款。还要注意,子查询在这里不是必需的,并且WHERE
子句与ON
合同条款JOIN
.所以:
旁注:你为什么要使用
json
而不是jsonb
? 后者提供了更多的功能,应该是普遍的首选。你可以用jsonb_build_objet()
以及jsonb_agg()
而不是相应的json_*
功能。