如何在PostgreSQL中合并两列整数?

avi*_*avi 3 sql postgresql

我有以下查询:

select col1, col2
from ...
where...
Run Code Online (Sandbox Code Playgroud)

这使:

col1  col2
5
       17
4       5
12
5
       20
4      17
2       3
Run Code Online (Sandbox Code Playgroud)

我想将其转换为没有重复的一列,如下所示:

col3
5
17
4
12
20
2
3
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?我读了这个主题,将两列合并并添加到一个新列中,但这不是我所需要的...操作符||在这里没有帮助。

编辑: col3只是出现在col2col1中的所有数字的列表。

Vao*_*sun 5

select coalesce(col1,col2) 
from table 
Run Code Online (Sandbox Code Playgroud)

它将使用 col1,如果 col1 为 null,则 col2

https://www.postgresql.org/docs/current/static/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL

COALESCE 函数返回第一个不为空的参数。仅当所有参数均为 null 时才返回 Null。当检索数据进行显示时,它通常用于替换空值的默认值


Oto*_*dze 5

看来你需要 union

select col1 as col3 from t where col1 is not null
union
select col2 as col3 from t where col2 is not null
Run Code Online (Sandbox Code Playgroud)