SQL 如何获取不包含产品的类别?
SQL How to get categories that don't contain products?
SELECT DISTINCT {c2.name}
FROM
{BrandCategory AS c
JOIN CategoryProductRelation AS c2p
ON {c.pk} = {c2p.source}
JOIN Product AS p
ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2
ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c2 ON {c2.pk} = {c2p2.source}
JOIN CatalogVersion AS cat
ON {c.catalogVersion} = {cat.PK}}
WHERE
{c.code} = 'brand-MCH'
AND{cat.version} = 'Online'
AND {c2.code} NOT LIKE'%webFamily%'
AND {p.code} IN ('35365','34299')
以上查询提取了包含产品“35365”和“34299”的分类类别
Result:
Passenger
Touring
All-Season
Truck
All Categories:
All-Season
Performance
Passenger
Winter
Touring
Summer
Truck
All-Terrain
Competition
Lawn
现在的要求是提取所有其他分类类别,这意味着那些不包含产品“35365”和“34299”的类别
尝试 1:(失败)(不在)
SELECT DISTINCT {c2.name}
FROM {BrandCategory AS c
JOIN CategoryProductRelation AS c2p ON {c.pk} = {c2p.source}
JOIN Product AS p ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2 ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c2 ON {c2.pk} = {c2p2.source}
JOIN CatalogVersion AS cat ON {c.catalogVersion} = {cat.PK}}
WHERE {c.code} = 'brand-MCH' AND{cat.version} = 'Online'
AND {c2.code} NOT LIKE'%webFamily%'
AND {p.code} NOT IN ('35365','34299')
尝试 2:(失败)(不存在)
SELECT DISTINCT {c2.name}
FROM {BrandCategory AS c
JOIN CategoryProductRelation AS c2p ON {c.pk} = {c2p.source}
JOIN Product AS p ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2 ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c2 ON {c2.pk} = {c2p2.source}
JOIN CatalogVersion AS cat ON {c.catalogVersion} = {cat.PK}}
WHERE {c.code} = 'brand-MCH' AND{cat.version} = 'Online'
AND {c2.code} NOT LIKE'%webFamily%'
AND {c2.code}
AND NOT EXISTS ({{
SELECT DISTINCT {c3.name}
FROM {BrandCategory AS c
JOIN CategoryProductRelation AS c2p ON {c.pk} = {c2p.source}
JOIN Product AS p ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2 ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c3 ON {c3.pk} = {c2p2.source}
JOIN CatalogVersion AS cat ON {c.catalogVersion} = {cat.PK}}
WHERE {c3.name}={c2.name}
AND {c.code} = 'brand-MCH' AND{cat.version} = 'Online'
AND {c3.code} NOT LIKE'%webFamily%'
AND {p.code} IN ('35365','34299')}}
)
尝试 3:(失败)(不在子查询中)
SELECT DISTINCT {c2.name}
FROM {BrandCategory AS c
JOIN CategoryProductRelation AS c2p ON {c.pk} = {c2p.source}
JOIN Product AS p ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2 ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c2 ON {c2.pk} = {c2p2.source}
JOIN CatalogVersion AS cat ON {c.catalogVersion} = {cat.PK}}
WHERE {c.code} = 'brand-MCH' AND{cat.version} = 'Online'
AND {c2.code} NOT LIKE'%webFamily%'
AND {c2.name} NOT IN ({{
SELECT DISTINCT {c2.name}
FROM {BrandCategory AS c
JOIN CategoryProductRelation AS c2p ON {c.pk} = {c2p.source}
JOIN Product AS p ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2 ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c2 ON {c2.pk} = {c2p2.source}
JOIN CatalogVersion AS cat ON {c.catalogVersion} = {cat.PK}}
WHERE {c.code} = 'brand-MCH' AND{cat.version} = 'Online'
AND {c2.code} NOT LIKE'%webFamily%'
AND {p.code} IN ('35365','34299')}}
)
...
Result:
All-Season
Performance
Passenger
Winter
Touring
Summer
Truck
All-Terrain
Competition
Lawn
Expected:
Performance
Winter
Summer
All-Terrain
Competition
Lawn
请帮助获取那些不包含最佳查询的类别,以控制性能。
此外,如果有一种方法可以让所有类别在结果中带有一些标志来区分哪些产品包含真实或不包含,那将是绝对的黄金,因为在这里我们两次点击 DB 以获取包含然后包含的类别通过点播电话不包含的类别
注意:这些本质上是 SQL 查询,但只是用这些大括号稍作修改以支持 Hybris 框架中的灵活搜索查询格式
查询-
1.使用NOT IN-
SELECT {c.code}
FROM
{Product as p
join CategoryProductRelation as c2p on {c2p.target} = {p.pk}
join Category as c on {c2p.source} = {c.pk}}
WHERE
{p.code} NOT IN ('35365','34299')
使用 NOT EXISTS-
SELECT {c.code}
FROM
{CategoryProductRelation as c2p
join Category as c on {c2p.source} = {c.pk}}
WHERE
NOT EXISTS ({{SELECT {p.code} FROM {Product as p} WHERE {p.code} IN ('35365','34299') and {c2p.target}={p.pk}}})
使用子查询-
SELECT {c.code}
FROM
{Product as p
join CategoryProductRelation as c2p on {c2p.target} = {p.pk}
join Category as c on {c2p.source} = {c.pk}}
WHERE
{p.pk} IN ({{SELECT {pk} FROM {Product} WHERE {code} IN ('35365','34299')}})
首先查看 NOT EXISTS
和 NOT IN
是否给出正确的值。
对于 EXISTS
,请不要为 DISTINCT
或指定列而烦恼。这足够了:
NOT EXISTS ( SELECT 1 FROM ... )
删除所有与问题无关的JOIN
。这样可以更容易地看出问题所在。
牙套是什么?也许您应该删除 [mysql]
标签?
我觉得太简单了:
使用 NORTHWND
-- 首先:查询到select类没有任何产品
select Categories.CategoryID , 类别名称 , ProductID, ProductName
从 Products right join Categories on Categories.CategoryID = Products.CategoryID
其中(ProductID 为空)
--second : 查询 select 产品不属于任何类别
select Categories.CategoryID , 类别名称 , ProductID, ProductName
从离开的产品加入 Categories.CategoryID = Products.CategoryID 上的类别
其中(Categories.CategoryID 为空)
SELECT DISTINCT {c2.name}
FROM
{BrandCategory AS c
JOIN CategoryProductRelation AS c2p
ON {c.pk} = {c2p.source}
JOIN Product AS p
ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2
ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c2 ON {c2.pk} = {c2p2.source}
JOIN CatalogVersion AS cat
ON {c.catalogVersion} = {cat.PK}}
WHERE
{c.code} = 'brand-MCH'
AND{cat.version} = 'Online'
AND {c2.code} NOT LIKE'%webFamily%'
AND {p.code} IN ('35365','34299')
以上查询提取了包含产品“35365”和“34299”的分类类别
Result:
Passenger
Touring
All-Season
Truck
All Categories:
All-Season
Performance
Passenger
Winter
Touring
Summer
Truck
All-Terrain
Competition
Lawn
现在的要求是提取所有其他分类类别,这意味着那些不包含产品“35365”和“34299”的类别
尝试 1:(失败)(不在)
SELECT DISTINCT {c2.name}
FROM {BrandCategory AS c
JOIN CategoryProductRelation AS c2p ON {c.pk} = {c2p.source}
JOIN Product AS p ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2 ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c2 ON {c2.pk} = {c2p2.source}
JOIN CatalogVersion AS cat ON {c.catalogVersion} = {cat.PK}}
WHERE {c.code} = 'brand-MCH' AND{cat.version} = 'Online'
AND {c2.code} NOT LIKE'%webFamily%'
AND {p.code} NOT IN ('35365','34299')
尝试 2:(失败)(不存在)
SELECT DISTINCT {c2.name}
FROM {BrandCategory AS c
JOIN CategoryProductRelation AS c2p ON {c.pk} = {c2p.source}
JOIN Product AS p ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2 ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c2 ON {c2.pk} = {c2p2.source}
JOIN CatalogVersion AS cat ON {c.catalogVersion} = {cat.PK}}
WHERE {c.code} = 'brand-MCH' AND{cat.version} = 'Online'
AND {c2.code} NOT LIKE'%webFamily%'
AND {c2.code}
AND NOT EXISTS ({{
SELECT DISTINCT {c3.name}
FROM {BrandCategory AS c
JOIN CategoryProductRelation AS c2p ON {c.pk} = {c2p.source}
JOIN Product AS p ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2 ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c3 ON {c3.pk} = {c2p2.source}
JOIN CatalogVersion AS cat ON {c.catalogVersion} = {cat.PK}}
WHERE {c3.name}={c2.name}
AND {c.code} = 'brand-MCH' AND{cat.version} = 'Online'
AND {c3.code} NOT LIKE'%webFamily%'
AND {p.code} IN ('35365','34299')}}
)
尝试 3:(失败)(不在子查询中)
SELECT DISTINCT {c2.name}
FROM {BrandCategory AS c
JOIN CategoryProductRelation AS c2p ON {c.pk} = {c2p.source}
JOIN Product AS p ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2 ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c2 ON {c2.pk} = {c2p2.source}
JOIN CatalogVersion AS cat ON {c.catalogVersion} = {cat.PK}}
WHERE {c.code} = 'brand-MCH' AND{cat.version} = 'Online'
AND {c2.code} NOT LIKE'%webFamily%'
AND {c2.name} NOT IN ({{
SELECT DISTINCT {c2.name}
FROM {BrandCategory AS c
JOIN CategoryProductRelation AS c2p ON {c.pk} = {c2p.source}
JOIN Product AS p ON {c2p.target} = {p.pk}
JOIN CategoryProductRelation AS c2p2 ON {p.pk} = {c2p2.target}
JOIN TaxonomyCategory AS c2 ON {c2.pk} = {c2p2.source}
JOIN CatalogVersion AS cat ON {c.catalogVersion} = {cat.PK}}
WHERE {c.code} = 'brand-MCH' AND{cat.version} = 'Online'
AND {c2.code} NOT LIKE'%webFamily%'
AND {p.code} IN ('35365','34299')}}
)
...
Result:
All-Season
Performance
Passenger
Winter
Touring
Summer
Truck
All-Terrain
Competition
Lawn
Expected:
Performance
Winter
Summer
All-Terrain
Competition
Lawn
请帮助获取那些不包含最佳查询的类别,以控制性能。
此外,如果有一种方法可以让所有类别在结果中带有一些标志来区分哪些产品包含真实或不包含,那将是绝对的黄金,因为在这里我们两次点击 DB 以获取包含然后包含的类别通过点播电话不包含的类别
注意:这些本质上是 SQL 查询,但只是用这些大括号稍作修改以支持 Hybris 框架中的灵活搜索查询格式
查询-
1.使用NOT IN-SELECT {c.code}
FROM
{Product as p
join CategoryProductRelation as c2p on {c2p.target} = {p.pk}
join Category as c on {c2p.source} = {c.pk}}
WHERE
{p.code} NOT IN ('35365','34299')
使用 NOT EXISTS-
SELECT {c.code} FROM {CategoryProductRelation as c2p join Category as c on {c2p.source} = {c.pk}} WHERE NOT EXISTS ({{SELECT {p.code} FROM {Product as p} WHERE {p.code} IN ('35365','34299') and {c2p.target}={p.pk}}})
使用子查询-
SELECT {c.code} FROM {Product as p join CategoryProductRelation as c2p on {c2p.target} = {p.pk} join Category as c on {c2p.source} = {c.pk}} WHERE {p.pk} IN ({{SELECT {pk} FROM {Product} WHERE {code} IN ('35365','34299')}})
首先查看 NOT EXISTS
和 NOT IN
是否给出正确的值。
对于 EXISTS
,请不要为 DISTINCT
或指定列而烦恼。这足够了:
NOT EXISTS ( SELECT 1 FROM ... )
删除所有与问题无关的JOIN
。这样可以更容易地看出问题所在。
牙套是什么?也许您应该删除 [mysql]
标签?
我觉得太简单了:
使用 NORTHWND
-- 首先:查询到select类没有任何产品
select Categories.CategoryID , 类别名称 , ProductID, ProductName 从 Products right join Categories on Categories.CategoryID = Products.CategoryID 其中(ProductID 为空)
--second : 查询 select 产品不属于任何类别
select Categories.CategoryID , 类别名称 , ProductID, ProductName 从离开的产品加入 Categories.CategoryID = Products.CategoryID 上的类别 其中(Categories.CategoryID 为空)