对于这种类型的查询,你要开始一个“驱动程序”子查询产生的所有可能的组合。然后,做一个left outer join
到存在的,并选择不匹配的:
select driver.hid, driver.dept
from (select hid, dept
from (select distinct hid from hd) h cross join
(select distinct dept from hd) d
) driver left outer join
hd
on driver.hid = hd.hid and
driver.dept = hd.dept
where hd.hid is null;
编辑:
这与数据修正查询:
with hd as (
select 5 as hid, 'neuro' as dept union all
select 2, 'derma' union all
select 3, 'cardio' union all
select 2, 'ent' union all
select 1, 'neuro' union all
select 5, 'optha' union all
select 3, 'ent' union all
select 3, 'optha' union all
select 4, 'derma' union all
select 1, 'optha' union all
select 5, 'derma'
)
select driver.hid, driver.dept
from (select hid, dept
from (select distinct hid from hd) h cross join
(select distinct dept from hd)d
) driver left outer join
hd
on driver.hid = hd.hid and
driver.dept = hd.dept
where hd.hid is null;
注意它会返回一个比问题长的列表。我认为这份名单不完整。
你怎么知道它没有? –