0

我试图创建一个表custom_table

产品没有价格列,PC 和笔记本电脑都没有类型列。

所以我需要知道如何在这种情况下使用 with 子句创建表?

这是我的查询

with custom_table(model,type,price)
as
(select model,type from product
union
select model,price from pc
union 
select model,price from laptop)
select * from custom_table
4

1 回答 1

2

这不是公用表表达式的问题,而是联合的问题。
为了使联合语句起作用,您必须具有相同数量的具有相同数据类型的列。
我的猜测是你需要这样的东西:

with custom_table(model,type,price)
as
(select model,type, null as price from product
union
select model, null, price from pc
union 
select model, null, price from laptop)

select * from custom_table
于 2015-06-29T17:34:54.243 回答