如果我有以下样本表(按ID排序)
ID Date Type
-- ---- ----
1 01/01/2000 A
2 22/04/1995 A
2 14/02/2001 B
您可以立即看到ID=1
没有Type=B
但ID=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行!)
这是否有一般性解决方案?
我可以将表连接到自身并以这种方式进行吗?
答案 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;