PostgreSQL可以从函数返回的数组中选择吗?

时间:2022-02-14 01:45:04

This query fails:

此查询失败:

SELECT xpath('/my/xpath/expr', my_xml)[1] FROM my_table

ERROR:  syntax error at or near "["

But this one works:

但是这个有效:

SELECT x[1] FROM
    (SELECT xpath('/my/xpath/expr', my_xml) as x FROM my_table) as ss

My xpath expression always returns only a single value, but the Postgres xpath function returns an array. I want to select the first value in the array. While the subselect works, it's pretty ugly.

我的xpath表达式总是只返回一个值,但Postgres xpath函数返回一个数组。我想选择数组中的第一个值。虽然subselect有效,但它非常难看。

Why doesn't the first query work, and is there a cleaner way to do this than the second query?

为什么第一个查询不起作用,是否有比第二个查询更简洁的方法?

1 个解决方案

#1


7  

How about this:

这个怎么样:

SELECT (xpath('/my/xpath/expr', my_xml))[1] FROM my_table;

#1


7  

How about this:

这个怎么样:

SELECT (xpath('/my/xpath/expr', my_xml))[1] FROM my_table;