我有一个表名“数字”。我有两列,即。Col_1 & Col_2
现在,我想编写一个查询,以便可以在单列 Col_3 中显示上述两列的值,如下所示。
你可以使用联合
SELECT Col_1 AS Col_3
FROM Numbers
UNION
SELECT Col_2 AS Col_3
FROM Numbers
如果您只想要两个表中的唯一值并且不介意扫描表两次,那么:
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