当前位置:  开发笔记 > 数据库 > 正文

ROWS FROM()中具有多个嵌套调用的列定义

如何解决《ROWSFROM()中具有多个嵌套调用的列定义》经验,为你挑选了1个好方法。

我想在select子句中使用多个数组。显而易见的一个没有用,postgresql指向ROWS FROM()...

select * from unnest(array[1,2], array[3,4]) as (a int, b int);

错误:

UNNEST() with multiple arguments cannot have a column definition list  
LINE 1: select * from unnest(array[1,2], array[3,4]) as (a int, b in...
                                                         ^
HINT:  Use separate UNNEST() calls inside ROWS FROM(), and attach a column definition list to each one.

...

select * from rows from (unnest(array[1,2]), unnest(array[3,4])) as (a int, b int);

错误:

ROWS FROM() with multiple functions cannot have a column definition list  
LINE 1: ...from (unnest(array[1,2]), unnest(array[3,4])) as (a int, b i...
                                                             ^
HINT:  Put a separate column definition list for each function inside ROWS FROM().

手册也对此进行了说明,但是如何定义这些“单独的列定义”?



1> Kristján..:

您可以只使用以下名称定义列名称,而不使用它们的类型AS t(a, b)

#= SELECT * FROM unnest(array[1,2], array[3,4,5]) AS t(a, b);
 a | b
---+---
 1 | 3
 2 | 4
 ? | 5

要定义类型,请对数组本身进行操作:

#= SELECT a / 2 AS half_a, b / 2 AS half_b
   FROM unnest(array[1,2]::float[], array[3,4,5]::integer[]) AS t(a, b);
 half_a | half_b
--------+--------
    0.5 |      1
      1 |      2
      ? |      2

推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有