2

我从附加模式中的查询要求我寻找测试呈阳性的人去的相同位置,并且与未测试的人在同一个人中。(未测试表示测试表中不存在的人。

架构

--find the same locations of where the positive people and the untested people went  

select checkin.LocID, checkin.PersonID 
from checkin join testing on checkin.personid = testing.personid 
where results = 'Positive'
and  (select CheckIn.PersonID  
from checkin join testing on checkin.PersonID = testing.PersonID where CheckIn.PersonID
not in (select testing.PersonID from testing));

在我看来,查询说明了以下内容

从加入检查和测试表中选择一个位置和人员,结果是肯定的,并从检查表中选择一个人,而不是在测试表中。

由于我得到的答案是零,而且我手动知道有人。我究竟做错了什么?

我希望这是有道理的。

4

3 回答 3

2

您可以使用以下查询让人们测试为“阳性”:

select personid from testing where results = 'Positive'

和未经测试的人:

select p.personid 
from person p left join testing t 
on t.personid = p.personid
where t.testingid is null

您必须将这些查询的副本加入到每个查询中,checkin并且这些副本连接在一起:

select l.*
from (select personid from testing where results = 'Positive') p
inner join checkin cp on cp.personid = p.personid
inner join checkin cu on cu.lid = cp.lid
inner join (
  select p.personid 
  from person p left join testing t 
  on t.personid = p.personid
  where t.testingid is null
) pu on pu.personid = cu.personid
inner join location l on l.locationid = cu.lid
于 2020-08-20T10:52:51.370 回答
2

如果您想要的是阳性人员,他们所在的位置也有人未经测试,您可以考虑:

select ch.LocID,
       group_concat(case when t.results = 'positive' then ch.PersonID end) as positive_persons
from checkin ch left join
     testing t
     on ch.personid = t.personid 
group by ch.LocId
having sum(case when t.results = 'positive' then 1 else 0 end) > 0 and
       count(*) <> count(t.personid);  -- at least one person not tested

使用这种结构,您可以让未经测试的人使用:

group_concat(case when t.personid is null then ch.personid)
于 2020-08-20T11:10:54.043 回答
1

您有几个错误(缺少存在,存在中的独立子查询)。我相信这应该做的工作

select ch1.LocID, ch1.PersonID 
from checkin ch1
join testing t1 on ch1.personid = t1.personid 
where results = 'Positive'
and exists (
    select 1
    from checkin ch2    
    where ch1.LocID = ch2.LocID and ch2.PersonID not in (
        select testing.PersonID 
        from testing
    )
);
于 2020-08-20T10:48:51.730 回答