简体   繁体   English

如何在 SQL 中查找

[英]How to look up in SQL

I have a table named PERSON.我有一个名为 PERSON 的表。 Each person has a person ID and has an ADDITIONAL INFO table with 5 entries of types of contracts - C1 through C5.每个人都有一个人 ID,并有一个附加信息表,其中包含 5 个合同类型条目 - C1 到 C5。 Each contract will have a unique name.if person has not accepted a contract,then that contract will not show up in his ADDITIONAL INFO table.Eg:Person 1 has ADDITIONAL INFO Table with only 3 raws - C2-BLOWSY,C4-GEN,C5-OLJ.每个合同都有一个唯一的名称。如果人没有接受合同,那么该合同将不会显示在他的附加信息表中。例如:第 1 个人有附加信息表,只有 3 个原始数据 - C2-BLOWSY,C4-GEN, C5-OLJ。 This means Person 1 has not accepted contracts C1 and C3.这意味着人 1 没有接受合同 C1 和 C3。

I need the list of Persons who haven't accepted contract C1.我需要未接受合同 C1 的人员列表。

Problem is that i cannot put the condition as "Where C1 = NULL" since Raw C1 itself will not appear if person has not accepted that contract.Logic is that if ADDITIONAL INFO table don't have entry named C1,Then we can include this person in the list.But i don't know how to write query for this.问题是我不能将条件设置为“Where C1 = NULL”,因为如果人没有接受该合同,原始 C1 本身将不会出现。逻辑是如果附加信息表没有名为 C1 的条目,那么我们可以包括这个列表中的人。但我不知道如何为此编写查询。

Please help.Thanks in advance请帮助。在此先感谢

Assuming that table of contracts exist we have:假设存在合同表,我们有:

WITH PERSON AS    (
                   select 1 as id_person, 'John Doe 1' as name_person from dual union all  
                   select 2 as id_person, 'John Doe 2' as name_person from dual
                  )
    ,ADD_INFO AS  (
                   select 1 as id_person, 'C2-BLOWSY' as type_contract from dual union all
                   select 1 as id_person, 'C4-GEN'    as type_contract from dual union all
                   select 1 as id_person, 'C5-OLJ'    as type_contract from dual
                  )
    ,CONTRACTS AS (
                   select 'C1-IJK'    as type_contract from dual union all
                   select 'C2-BLOWSY' as type_contract from dual union all 
                   select 'C3-ADF'    as type_contract from dual union all
                   select 'C4-GEN'    as type_contract from dual union all
                   select 'C5-OLJ'    as type_contract from dual
                  )
      select p.id_person, p.name_person, c.type_contract
        from person p, 
             contracts c
       where c.type_contract = 'C1-IJK'   --Search by C1 missed     
       group by  p.id_person, p.name_person, c.type_contract
       having (select count(*) from add_info a where a.id_person = p.id_person and a.type_contract = c.type_contract) = 0
       order by p.id_person, c.type_contract;

 ID_PERSON NAME_PERSO TYPE_CONT
---------- ---------- ---------
         1 John Doe 1 C1-IJK   
         2 John Doe 2 C1-IJK   

Thank you谢谢

Create person and additional info tables:创建人员和附加信息表:

CREATE TABLE dbo.PERSON
    (
    PERSON_ID int NULL,
    PERSON_NAME varchar(50) NULL
    )  ON [PRIMARY]
GO

CREATE TABLE dbo.ADDL_INFO
    (
    PERSON_ID int NULL,
    CONTRACT varchar(10) NULL
    )  ON [PRIMARY]
GO

Insert the data to match your scenario:插入数据以匹配您的方案:

INSERT INTO dbo.PERSON VALUES (1, 'Person1');
INSERT INTO dbo.ADDL_INFO VALUES (1, 'C1');
INSERT INTO dbo.ADDL_INFO VALUES (1, 'C2');
INSERT INTO dbo.ADDL_INFO VALUES (1, 'C3');
INSERT INTO dbo.ADDL_INFO VALUES (1, 'C4');
INSERT INTO dbo.ADDL_INFO VALUES (1, 'C5');

INSERT INTO dbo.PERSON VALUES (2, 'Person2');
INSERT INTO dbo.ADDL_INFO VALUES (2, 'C2');
INSERT INTO dbo.ADDL_INFO VALUES (2, 'C4');
INSERT INTO dbo.ADDL_INFO VALUES (2, 'C5');

The below query will select the people that DO NOT have contract 'C1'.下面的查询将 select 没有合同“C1”的人。 Please note to make this reusable, the 'C1' hard-coded below should be replaced with a?请注意,为了使其可重用,下面硬编码的“C1”应替换为? and this could be used as a parameterized query in your application:这可以在您的应用程序中用作参数化查询:

SELECT PERSON_ID, PERSON_NAME FROM dbo.PERSON RSLT
WHERE NOT EXISTS (
    SELECT P.PERSON_ID FROM dbo.PERSON P
        INNER JOIN dbo.ADDL_INFO A ON A.PERSON_ID = P.PERSON_ID
    WHERE A.CONTRACT = 'C1' and RSLT.PERSON_ID = P.PERSON_ID
)

Results:结果:

在此处输入图像描述

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

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