SELECT WHERE 相关记录的值没有出现

SELECT WHERE related record's values have no occurrence

我有一个Clienttable和一个Devicestable。

一个客户端可以有多个设备,一个客户端和设备一样都处于活动状态。

我想要所有处于活动状态但没有活动设备的客户端。

我在客户端上为所有活动客户端加入了设备,因此客户端不止一次出现,但现在我不知道如何 select 所有没有活动设备的客户端。

我的人际关系是这样建立的:

客户:

pkClientID   Name     IsActive
------------------------------
1            Jake     1
2            Philip   1

设备

pkDeviceID  fkClientID  DeviceName  IsActive
---------------------------------------------
1           1           Samsung     1
2           1           Apple       0
3           2           Samsung     0
4           2           Sony        0

既然我已经有所有活跃的客户,我怎么才能 select 只有 Philip,因为他活跃但没有活跃的设备?

使用 not exists():

select *
from client c
where c.isactive = 1 
  and not exists (
    select 1
    from device d
    where d.fkclientid = c.pkclientid
      and d.isactive = 1
      )

rextester 演示:http://rextester.com/NCR78612

returns:

+------------+--------+----------+
| pkClientid |  Name  | IsActive |
+------------+--------+----------+
|          2 | Philip |        1 |
+------------+--------+----------+

使用not innot existsleft join/where:

select c.*
from client c
where c.IsActive = 1 and
      not exists (select 1
                  from devices d
                  where d.fkClientID = c.ClientID and d.IsActive = 1
                 );