Couchbase / N1QL - 获取所有不存在的绑定记录(内部连接的反向/反向)

问题描述 投票:1回答:1

查询以获取存在的绑定记录(实质上是存在的所有制造商缺陷):

select
      md.f_manufacturerId, md.f_defectId
  from productionlines as md
  inner join productionlines as m on m.type="manufacturer" and m.manufacturerId=md.f_manufacturerId
  inner join productionlines as d on d.type="defect" and d.defectId=md.f_defectId
  where md.type="manufacturerdefect"
  order by md.f_manufacturerId

我想要OPPOSITE / INVERSE这个...我想知道我们没有的DEFECT TO MANUFACTURER搭售清单。

数据结构......

制造商:

{
  "name": "Ball Bearings"
  "id": "00a4260956d54e46932001d853df843c",
  "manufacturerId": 28,
  "type": "manufacturer"
}


{
  "name": "Wheel Rims"
  "id": "3ad4b5c6433d6e8b9c230fdd5dda8b33",
  "manufacturerId": 2,
  "type": "manufacturer"
}

缺陷:

{
  "name": "Bad Drill Bit"
  "id": "c348fd358d10023964e45d6590624a00",
  "defectId": 7,
  "type": "defect"
}


{
  "name": "Bad Shipping"
  "id": "33b8add5ddf032c9b8e6d3346c5b4da3",
  "defectId": 9,
  "type": "defect"
}

制造商缺陷:

{
  "id": "de426435bd10023964e45d6590624a00",
  "f_defectId": 7,
  "f_manufacturerId": 2,
  "type": "manufacturerdefect"
}

{
  "id": "de426435bd10023964e45d6590624a01",
  "f_defectId": 9,
  "f_manufacturerId": 2,
  "type": "manufacturerdefect"
}

{
  "id": "de426435bd10023964e45d6590624a02",
  "f_defectId": 7,
  "f_manufacturerId": 28,
  "type": "manufacturerdefect"
}

{
  "id": "de426435bd10023964e45d6590624a03",
  "f_defectId": 9,
  "f_manufacturerId": 28,
  "type": "manufacturerdefect"
}

所以要明确的是,如果上面的任何制造商缺陷不存在,我想知道所有缺少的制造商和缺陷ID组合。

couchbase n1ql
1个回答
1
投票

第1步:找到所有组合。

第2步:排除所有现有的制造缺陷。

(select
    m.manufacturerId as manufacturerId, d.defectId as defectId
from productionlines as d
inner join productionlines as m on m.type="manufacturer"
where d.type="defect"
order by m.manufacturerId)
EXCEPT
(select
    md.f_manufacturerId as manufacturerId, md.f_defectId as defectId
from productionlines as md
where md.type="manufacturerdefect")
order by manufacturerId
© www.soinside.com 2019 - 2024. All rights reserved.