select p.Name, m.last_name from Product p
inner join manufacturer m on m.manufacturer_id = p.product_id
inner join productType pT on p.product_id = pT.type_id
where COUNT(distinct pT.type_id) >= 2
group by m.manufacturer_id
select * from manufacturer m
inner join manufacturer_has_product mhp
on m.manufacturer_id = m.manufacturer_id
inner join product p
on mhp.product_id = p.product_id
where m.manufacturer_id in (
select m.manufacturer_id
from manufacturer m
inner join manufacturer_has_product mhp
on m.manufacturer_id = m.manufacturer_id
inner join product p
on mhp.product_id = p.product_id
inner join productType_has_product pthp
on pt.product_product_id = p.product_id
inner join productType pt
on pt.productType_type_id = pt.type_id
group by m.manufacturer_id
having count(pt.type_id) > 1
)
SELECT tmp.last_name, p.name -- <- compose the final select
FROM
-- sub-select the underlying manufacturers (id + last_name):
(
-- distinct ... or join deeper
SELECT COUNT (DISTINCT php.productType_type_id), m.manufacturer_id, m.last_name
FROM manufacturer m
JOIN manufacturer_has_product mhp
ON (mhp.manufacturer_manufacturer_id = m.manufacturer_id)
JOIN Product p
ON (p.product_id = mhp.Product_product_id)
--- until "php" is enough, but needs distinct
JOIN productType_has_product php
ON (php.Product_product_id = p.product_id)
-- obligatory:
GROUP BY m.manufacturer_id, m.last_name
-- (aggregated) filter condition:
HAVING COUNT (DISTINCT php.productType_type_id) > 1
) tmp -- an alias for the sub-select
-- join for the final result:
JOIN manufacturer_has_product mhp
-- on TMP.manufacturer_id!
ON (mhp.manufacturer_manufacturer_id = tmp.manufacturer_id)
JOIN Product p ON (p.product_id = mhp.Product_product_id)
3条答案
按热度按时间8xiog9wr1#
没有任何数据很难查看,但这里有一个镜头。
n9vozmp42#
一种方法是在where子句中使用subselect来过滤select。我做得很快只是为了证明:
gdx19jrr3#
灵感来自@eric的答案,未经测试,评论中的解释。。。