在一列中合并两个不同列的字段值

时间:2013-07-27 08:33:43

标签: oracle10g

我有一个表名“Numbers”。我有两列即。 Col_1& Col_2

enter image description here

现在,我想编写一个查询,以便在单个列Col_3中显示上述两列的值,如下所示。

enter image description here

2 个答案:

答案 0 :(得分:2)

你可以使用UNION

SELECT Col_1 AS Col_3 
FROM Numbers

UNION

SELECT Col_2 AS Col_3 
FROM Numbers

答案 1 :(得分:1)

如果您只想要两个表中的唯一值,并且不介意扫描表两次,那么:

select col1 as col3
from   numbers
union
select col2
from   numbers

如果要保留所有值,请使用UNION ALL:

select col1 as col3
from   numbers
union all
select col2
from   numbers

如果表格足够大,则最好避免两次扫描:

with cte_two_rows as (
  select 1 col from dual union all
  select 2 col from dual)
select
  case col
    when 1 then col1
    when 2 then col2
  end col3
from
  numbers cross join cte_two_rows