选举:
EID PID PlanCode Coverage
2 49791 DELTA FAMILY
2 49791 LIGNA FAMILY
2 49791 BSP FAMILY
2 49792 BSP FAMILY
2 49792 LIGNA FAMILY
2 49792 DELTA FAMILY
2 49793 LIGNA FAMILY
关系表:
EID PID
2 49791
2 49792
2 49793
表格描述:
关系:员工有3个家属
选举:对于每个Pid,员工选择一个特定的计划
问题:
作为Pid:49793仅注册LigNa计划,尝试为剩余计划添加2行,即delta,BSP行
我们可以这样做,因为计划名称没有修复,但我们知道关系表中的家属吗?
请建议..
PS:它现在正在使用交叉连接
答案 0 :(得分:0)
如果没有错,您正尝试为每个PlanCode
组合插入缺少的EID,PID
。我不太确定LEFT JOIN
用法与此问题的关联,除非我遗漏了某些内容
INSERT INTO Election
(EID,PID,PlanCode,Coverage)
SELECT A.EID,A.PID,B.PlanCode,A.Coverage
FROM (SELECT DISTINCT EID,PID,Coverage
FROM Election) A
CROSS JOIN (SELECT DISTINCT PlanCode
FROM Election) B
WHERE NOT EXISTS (SELECT 1
FROM Election C
WHERE A.EID = C.EID
AND A.PID = C.PID
AND B.PlanCode = C.PlanCode)