generate_subscripts(array, 2) returns只有一个多维元素时记录两条
generate_subscripts(array, 2) returns two records when there is only one multidimensional element
为什么多维数组只有一个多维元素时return2记录images
?
SELECT images
FROM (
SELECT images, generate_subscripts(images, 2) AS s
FROM listings
WHERE listings.id = 2
) as foo;
注意:为了便于查看,我缩短了 base64 字符串。
id | 2
created_at | 2017-04-19 23:44:50.150913+00
posted_by | 10209280753550922
images | {{/9j/4AAJRgAB2dgKd/9k=,3/2/image-3-2-1492645490308.jpeg}}
dev_dolphin_db=# SELECT images FROM(SELECT images, generate_subscripts(images, 2) AS s FROM listings where listings.id = 2) as foo;
-[ RECORD 1 ]----------------------------------------------------------------------------------------------------------------------
images | {{/9j/4AAQSkZJRdgKd/9k=,3/2/image-3-2-1492645490308.jpeg}}
-[ RECORD 2 ]----------------------------------------------------------------------------------------------------------------------
images | {{/9j/4AAQSkZN2dgKd/9k=,3/2/image-3-2-1492645490308.jpeg}}
你的数组中有两个个元素,用逗号分隔:
{{/9j/4AAJRgAB2dgKd/9k=,3/2/image-3-2-1492645490308.jpeg}}
参见:
SELECT *
FROM unnest('{{/9j/4AAJRgAB2dgKd/9k=,3/2/image-3-2-1492645490308.jpeg}}'::text[])
unnest
--------------------------------
/9j/4AAJRgAB2dgKd/9k=
3/2/image-3-2-1492645490308.jpeg
generate_subscripts()
returns one row per element in the specified dimension (not one row per dimension). The manual:
generate_subscripts
is a convenience function that generates the set
of valid subscripts for the specified dimension of the given array.
Zero rows are returned for arrays that do not have the requested
dimension, or for NULL arrays (but valid subscripts are returned for
NULL array elements).
如果那应该是一个 单个 元素,则必须用双引号引起来以避开逗号的特殊含义:
{{"/9j/4AAJRgAB2dgKd/9k=,3/2/image-3-2-1492645490308.jpeg"}}
旁白:在现代 Postgres 中,您可以使用这个更简单的等效查询:
SELECT images
FROM listings, generate_subscripts(images, 2) s
WHERE id = 2;
这是隐含的 CROSS JOIN LATERAL
。参见:
为什么多维数组只有一个多维元素时return2记录images
?
SELECT images
FROM (
SELECT images, generate_subscripts(images, 2) AS s
FROM listings
WHERE listings.id = 2
) as foo;
注意:为了便于查看,我缩短了 base64 字符串。
id | 2
created_at | 2017-04-19 23:44:50.150913+00
posted_by | 10209280753550922
images | {{/9j/4AAJRgAB2dgKd/9k=,3/2/image-3-2-1492645490308.jpeg}}
dev_dolphin_db=# SELECT images FROM(SELECT images, generate_subscripts(images, 2) AS s FROM listings where listings.id = 2) as foo;
-[ RECORD 1 ]----------------------------------------------------------------------------------------------------------------------
images | {{/9j/4AAQSkZJRdgKd/9k=,3/2/image-3-2-1492645490308.jpeg}}
-[ RECORD 2 ]----------------------------------------------------------------------------------------------------------------------
images | {{/9j/4AAQSkZN2dgKd/9k=,3/2/image-3-2-1492645490308.jpeg}}
你的数组中有两个个元素,用逗号分隔:
{{/9j/4AAJRgAB2dgKd/9k=,3/2/image-3-2-1492645490308.jpeg}}
参见:
SELECT *
FROM unnest('{{/9j/4AAJRgAB2dgKd/9k=,3/2/image-3-2-1492645490308.jpeg}}'::text[])
unnest
--------------------------------
/9j/4AAJRgAB2dgKd/9k=
3/2/image-3-2-1492645490308.jpeg
generate_subscripts()
returns one row per element in the specified dimension (not one row per dimension). The manual:
generate_subscripts
is a convenience function that generates the set of valid subscripts for the specified dimension of the given array. Zero rows are returned for arrays that do not have the requested dimension, or for NULL arrays (but valid subscripts are returned for NULL array elements).
如果那应该是一个 单个 元素,则必须用双引号引起来以避开逗号的特殊含义:
{{"/9j/4AAJRgAB2dgKd/9k=,3/2/image-3-2-1492645490308.jpeg"}}
旁白:在现代 Postgres 中,您可以使用这个更简单的等效查询:
SELECT images
FROM listings, generate_subscripts(images, 2) s
WHERE id = 2;
这是隐含的 CROSS JOIN LATERAL
。参见: