使用NULL条件填充数据库

时间:2015-06-09 10:21:59

标签: sql database

如果我有以下样本表(按ID排序)

ID    Date         Type
--    ----         ----
1     01/01/2000   A
2     22/04/1995   A
2     14/02/2001   B

您可以立即看到ID=1没有Type=BID=2没有ID Date Type -- ---- ---- 1 01/01/2000 A 1 NULL B 2 22/04/1995 A 2 14/02/2001 B 。我想做什么,如果填写一行来表明这一点:

"

可能有100种不同类型,(如果他们缺少100种类型,可能需要每人最多插入100行!)

这是否有一般性解决方案?

我可以将表连接到自身并以这种方式进行吗?

1 个答案:

答案 0 :(得分:2)

您可以使用cross join生成所有行,使用left join来获取实际数据值:

select i.id, s.date, t.type
from (select distinct id from sample) i cross join
     (select distinct type from sample) t left join
     sample s
     on s.id = i.id and
        s.type = t.type;