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

时间:2019-04-04 16:54:37

标签: couchbase n1ql

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

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

我想要与此相反的... 我想知道我们没有的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"
}

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

1 个答案:

答案 0 :(得分: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