7

我正在尝试做这样的事情:

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

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

有没有办法从 select 语句中指定结果的行、列?

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

4

4 回答 4

7

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

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

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

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

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


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

于 2012-06-22T19:01:41.167 回答
2
select char_length(text_field) from "table" where key = 1
于 2012-06-22T18:57:17.450 回答
0

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

select char_length((select text_field from table where key = 1));
select char_length(text_field) from table where key = 1;
于 2012-06-22T19:13:57.707 回答
0

它应该是

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

我也相信,你的表名不是table.

于 2012-06-22T18:53:02.170 回答