简体   繁体   English

SQL:左外部联接中的条件

[英]SQL: Condition in Left outer Join

Election: 选举:

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

relationship Table: 关系表:

EID     PID
2   49791
2   49792
2   49793

table Desc: 表Desc:

Relationship: Employee has 3 dependents 关系:员工有3个家属

Election:For each Pid, an employee chooses a particular Plan 选举:员工为每个Pid选择一个特定的计划

Problem: 问题:

As Pid:49793 enroll in LigNa plan only, trying to add 2 rows for remaining plan ie delta,BSP row 由于Pid:49793仅注册LigNa计划,因此尝试为剩余计划添加2行,即增量,BSP行

Can we do it as plan name is not fixed, but we know of dependents in relationship table? 我们可以这样做,因为计划名称不固定,但是我们知道关系表中的依赖项吗?

Please suggest.. 请建议..

PS: Its working now using cross Join PS:现在可以使用交叉联接工作

If am not wrong you are trying to insert the missing PlanCode for each EID,PID combination. 如果没错,则尝试为每个EID,PID组合插入丢失的PlanCode Am not very sure about the link of LEFT JOIN usage with this question unless I missed something 除非我错过了一些内容,否则不太了解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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM