[英]Why is array_remove (POSTGRESQL) not working in this case?
This is not working correctly: SELECT array_remove(array_agg(s1->>'karten'),'8 eich.jpg') from spiele;
这无法正常工作: SELECT array_remove(array_agg(s1->>'karten'),'8 eich.jpg') from spiele;
The output: ["3 eich.jpg","8 eich.jpg","5 sche.jpg","2 herz.jpg","1 laub.jpg","4 eich.jpg","2 sche.jpg","5 laub.jpg","4 herz.jpg","4 sche.jpg"]
output: ["3 eich.jpg","8 eich.jpg","5 sche.jpg","2 herz.jpg","1 laub.jpg","4 eich.jpg","2 sche.jpg","5 laub.jpg","4 herz.jpg","4 sche.jpg"]
The datatype of s1 is json; s1的数据类型是json; s1->>'karten' is an array s1->>'karten' 是一个数组
If karten
refers to a JSON array in the, then s1 ->> 'karten
doesn't return each element individually, but a one string representing the array.如果karten
指的是 JSON 数组,那么s1 ->> 'karten
不会单独返回每个元素,而是一个表示数组的字符串。 So array_agg()
doesn't really aggregate multiple values - only one.所以array_agg()
并没有真正聚合多个值——只有一个。 The result is an array with a single element - that happens to look like a JSON array.结果是一个具有单个元素的数组——恰好看起来像一个 JSON 数组。
You can remove an element from a JSON array if the values is jsonb
(the recommended data type to handle JSON in Postgres anyway) using the -
operator:如果值为jsonb
(无论如何在 Postgres 中处理 JSON 的推荐数据类型),则可以使用-
运算符从 JSON 数组中删除元素:
select (s1 -> 'karten')::jsonb - '8 eich.jpg'
will return a jsonb
value that is an without the key '8 eich.jpg'
.将返回一个没有键'8 eich.jpg'
的jsonb
值。
Unfortunately there is no easy conversion from a JSON array to a native array.不幸的是,从 JSON 阵列到本机阵列的转换并不容易。 Search this site, there are multiple answers for that.搜索这个网站,有多个答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.