在函数中使用查询结果(postgres 8.3)

时间:2021-07-14 15:44:37

I am trying to do something like this:

我想做这样的事情:

select char_length(select text_field from table where key = 1)

This won't work, and I presume, because the return type of a query is a table, not a string or text variable.

这是行不通的,我认为,因为查询的返回类型是表,而不是字符串或文本变量。

Is there a way to specify row,col of the result from a select statement?

有没有办法从select语句中指定结果的row,col?

edit: I overlooked to mention, that char_length is a function.

编辑:我忽略了,char_length是一个函数。

4 个解决方案

#1


8  

When passing the result of a query to a function, simply wrap the query in brackets:

将查询结果传递给函数时,只需将查询包装在括号中:

select char_length((select text_field from table where key = 1));

The outer set of brackets is for the function, the inner set converts a query to a result.

外部括号集用于函数,内部集合将查询转换为结果。

This syntax is not specific to postgres - it applies to all SQL servers.

此语法不是特定于postgres - 它适用于所有SQL服务器。

This link shows the above code executing correctly (thanks to @Fahim Parkar for this)

此链接显示上面的代码正确执行(感谢@Fahim Parkar)


Although, you could re-factor your query to not require this syntax, nevertheless this is how you "pass the result of a query to a function".

虽然,您可以将查询重新分解为不需要此语法,但这就是“将查询结果传递给函数”的方法。

#2


2  

select char_length(text_field) from "table" where key = 1

#3


0  

Assuming key is a primary key or unique key the first example below will work. It works only if the sub-query returns only 1 row. The second example will work for 1 or more rows.

假设密钥是主密钥或唯一密钥,下面的第一个示例将起作用。仅当子查询仅返回1行时,它才有效。第二个示例适用于1行或更多行。

select char_length((select text_field from table where key = 1));
select char_length(text_field) from table where key = 1;

#4


0  

It should be

它应该是

select char_length(text_field) from "table" where key = 1

Also I believe, your table name is not table.

我相信,你的表名不是表。

#1


8  

When passing the result of a query to a function, simply wrap the query in brackets:

将查询结果传递给函数时,只需将查询包装在括号中:

select char_length((select text_field from table where key = 1));

The outer set of brackets is for the function, the inner set converts a query to a result.

外部括号集用于函数,内部集合将查询转换为结果。

This syntax is not specific to postgres - it applies to all SQL servers.

此语法不是特定于postgres - 它适用于所有SQL服务器。

This link shows the above code executing correctly (thanks to @Fahim Parkar for this)

此链接显示上面的代码正确执行(感谢@Fahim Parkar)


Although, you could re-factor your query to not require this syntax, nevertheless this is how you "pass the result of a query to a function".

虽然,您可以将查询重新分解为不需要此语法,但这就是“将查询结果传递给函数”的方法。

#2


2  

select char_length(text_field) from "table" where key = 1

#3


0  

Assuming key is a primary key or unique key the first example below will work. It works only if the sub-query returns only 1 row. The second example will work for 1 or more rows.

假设密钥是主密钥或唯一密钥,下面的第一个示例将起作用。仅当子查询仅返回1行时,它才有效。第二个示例适用于1行或更多行。

select char_length((select text_field from table where key = 1));
select char_length(text_field) from table where key = 1;

#4


0  

It should be

它应该是

select char_length(text_field) from "table" where key = 1

Also I believe, your table name is not table.

我相信,你的表名不是表。