【问题标题】:Need more info on SQL "WITH" clause [duplicate]需要有关 SQL“WITH”子句的更多信息 [重复]
【发布时间】:2015-06-29 16:55:32
【问题描述】:

我试图创建一个表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

【问题讨论】:

    标签: sql-server with-clause


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 2014-12-31
      相关资源
      最近更新 更多