Sql 查询不 return 在不同 table 的列中具有特定值的用户

Sql query to not return the user who has a particular value in an column of a different table

所以我在这个 sql 查询 (MYSQL) 中遇到了一些问题,我不想 return 如果其中一个列在另一个 [=74] 中,则用户记录=]包含某个值。

这是 table 的结构:

table 的描述:

  1. 这个人 table 是不言自明的。它只包含关于我 returning

  2. 这个人的各种信息
  3. 个人属性 table 描述了一个人可能具有的属性及其值和类型,其中 person_attribute_type_id 是外键

  4. 人物属性类型描述了不同的属性类型。

现在我有一个名为 PATIENT_STATE 的 person_attribute_type 即在 person_attribute_type table 中有一个包含 name 列的记录,其值为 PATIENT_STATE

我不想 return 任何具有类型 PATIENT_STATE 的人物属性的人,而是所有其他人。

以下是我写的sql查询:

    select 
       p.uuid as uuid, pat.value as Atribute, p.person_id as personId,
       p.gender as gender, p.birthdate as birthDate, p.death_date as 
       deathDate, p.birthdate_estimated as birthdateEstimated,
       p.date_created as dateCreated
    from person p 

       left join person_name pn on pn.person_id = p.person_id
       JOIN person_attribute pat on p.person_id=pat.person_id and 
       pat.voided = 'false' and pat.person_attribute_type_id NOT IN 
        (SELECT person_attribute_type_id from person_attribute_type 
        where name='PATIENT_STATE') 
    where p.voided = 'false' and pn.voided = 'false' and ( 
    concat_ws(' ',coalesce(given_name), coalesce(middle_name), 
    coalesce(family_name)) like  '%Aud%') group by  p.person_id;

在 sql 查询中 %Aud% 就像我的搜索查询

如果我 运行 上面的查询,它不会 return 这个人拥有 PATIENT_STATE 的记录,但它 return 是一个说人物属性的记录是 XXX。问题是我想检查这个人是否包含 PATIENT_STATE 作为他的属性。如果有,我不想return那个人的记录,即使它包含任何其他属性类型。

我该怎么做?

I do not want to return any persons, who have have a Person Attribute of type PATIENT_STATE, but all others.

根据您的描述,这听起来像是一个 not exists 查询:

select p.*
from person p
where not exists (select 1
                  from person_attribute pa join
                       person_attribute_type pat
                       on pat.person_attribute_type_id = pa.person_attribute_type_id
                  where pat.name = 'PATIENT_STATE' and
                        pa.person_id = p.person_id
                 );

如果需要,您也可以将其表示为 left joins:

select p.*
from person p left join
     person_attribute pa
     on pa.person_id = p.person_id left join
     person_attribute_type pat
     on pat.person_attribute_type_id = pa.person_attribute_type_id and
        pat.name = 'PATIENT_STATE' 
where pat.person_attribute_type_id is null;

您的查询正在从 person_attribute_type 返回一个值。我认为这没有意义,因为您的问题是针对缺少的属性。