PostgreSQL:选择具有与另一个表中的所有条目对应的条目的所有类型
我有两张桌子,制作人和滑雪板。制作人有他们的ID,每个型号的滑雪板都知道其制作人的ID,每个型号也有一个类型,这在滑雪板中不是唯一的。我需要选择所有生产商生产的所有类型(不考虑型号)。我写了一个问题:PostgreSQL:选择具有与另一个表中的所有条目对应的条目的所有类型,sql,postgresql,select,relational-division,Sql,Postgresql,Select,Relational Division,我有两张桌子,制作人和滑雪板。制作人有他们的ID,每个型号的滑雪板都知道其制作人的ID,每个型号也有一个类型,这在滑雪板中不是唯一的。我需要选择所有生产商生产的所有类型(不考虑型号)。我写了一个问题: select type from skis s where not exists ( select name from producers p except ( select name from producers p w
select type
from skis s
where not exists (
select name from producers p
except
(
select name
from producers p
where (p.name=s.producer)
)
);
它只有在我有一个滑雪板和一个制作人的情况下才有效。这样做的好方法是什么
编辑以澄清:在producer表中,“name”列是他们的ID,而在ski表中producers ID列是“producer”。计算每种类型的制片人数量,并与制片人总数进行比较:
select type
from skis
group by type
having count(distinct producer) = (select count(*) from producers);
对你有用吗
select s.type
from
(
select type,
count(distinct producer) amount_producers_for_type
from skis
group by type
) s
inner join (
select count(distinct name) number_of_producers
from producers
) t
on t.number_of_producers = s.amount_producers_for_type