从双表创建表

nbysray5  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(222)

如何在oracle中创建下表。

+------+------+
| col1 | col2 |
+------+------+
| A    |    1 |
| B    |    2 |
+------+------+

我需要它作为with子句中的itermediate表。

km0tfn4u

km0tfn4u1#

您可以使用:

with t as (
      select 'A' as col1, 1 as col2 union all
      select 'B' as col1, 2 as col2
     )

然后你可以使用 t 在查询的其余部分。

kmynzznz

kmynzznz2#

请使用下面的查询,

with tbl as 
  (
   select 'A' as col1, 1 as col2 from dual
   UNION
   select 'B' as col1, 2 as col2 from dual
)

select * from tbl;
yzxexxkh

yzxexxkh3#

创建这样的公共表表达式时,可以在cte定义中命名列,这样可以避免将列分散在select语句中:

WITH cteWork (COL1, COL2) AS
  (SELECT 'A', 1 FROM DUAL UNION ALL
   SELECT 'B', 2 FROM DUAL)
SELECT *
  FROM cteWork

db<>在这里摆弄

相关问题