PostgreSQL:select 具有与另一个 table 中的所有条目相对应的条目的所有类型
PostgreSQL: select all types that have an entry corresponding to all entries in another table
我有两个 table,制作人和滑雪板。生产者有ID,每款滑雪板都知道其生产者的ID,每款也有类型,这在滑雪板中不是唯一的。我需要 select 所有生产商生产的所有类型(无论型号)。我写了一个查询:
select type
from skis s
where not exists (
select name from producers p
except
(
select name
from producers p
where (p.name=s.producer)
)
);
只有当我有 1 个滑雪板和 1 个生产者时它才有效。这样做的好方法是什么?
编辑澄清:在生产者 table 中,列 'name' 是他们的 ID,而在滑雪 table 中,生产者 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
我有两个 table,制作人和滑雪板。生产者有ID,每款滑雪板都知道其生产者的ID,每款也有类型,这在滑雪板中不是唯一的。我需要 select 所有生产商生产的所有类型(无论型号)。我写了一个查询:
select type
from skis s
where not exists (
select name from producers p
except
(
select name
from producers p
where (p.name=s.producer)
)
);
只有当我有 1 个滑雪板和 1 个生产者时它才有效。这样做的好方法是什么?
编辑澄清:在生产者 table 中,列 'name' 是他们的 ID,而在滑雪 table 中,生产者 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