获取多对多中未映射记录的数量 table
Get count of unmapped records in a many-to-many table
我有 3 个这样的表
产品
ProductID
ProductDetails
1
...
2
...
3
...
供应商
VendorID
VendorDetails
1
...
2
...
3
...
产品供应商
ProductID
VendorID
1
1
2
1
1
2
2
2
3
2
我将如何找到未映射到特定供应商的产品数量。
我试过了:
SELECT
COUNT(pr.id) AS product_count
FROM
products pr
LEFT JOIN vendor_product_map vp ON
pr.id = vp.product
LEFT JOIN vendors vv ON
vp.vendor = vv.id
WHERE
vv.id = 3 AND vp.vendor IS NULL
但这似乎不对。感谢任何帮助
一个简单的 not exists
查询就足够了:
select *
from products
where not exists (
select *
from vendor_product_map
where vendor_product_map.product = product.id
and vendor_product_map.vendor = 12345678
)
我有 3 个这样的表
产品
ProductID | ProductDetails |
---|---|
1 | ... |
2 | ... |
3 | ... |
供应商
VendorID | VendorDetails |
---|---|
1 | ... |
2 | ... |
3 | ... |
产品供应商
ProductID | VendorID |
---|---|
1 | 1 |
2 | 1 |
1 | 2 |
2 | 2 |
3 | 2 |
我将如何找到未映射到特定供应商的产品数量。
我试过了:
SELECT
COUNT(pr.id) AS product_count
FROM
products pr
LEFT JOIN vendor_product_map vp ON
pr.id = vp.product
LEFT JOIN vendors vv ON
vp.vendor = vv.id
WHERE
vv.id = 3 AND vp.vendor IS NULL
但这似乎不对。感谢任何帮助
一个简单的 not exists
查询就足够了:
select *
from products
where not exists (
select *
from vendor_product_map
where vendor_product_map.product = product.id
and vendor_product_map.vendor = 12345678
)