选择一种适用于所有干预措施的产品

时间:2018-09-28 15:28:10

标签: sql

您好,我的问题对您中的某些人很简单^^

我有一个表格产品,参考资料和干预信息。进行干预时,表格参考会在干预所需的产品与干预之间建立联系。

我想知道如何搜索参与所有干预措施的产品。

这是我的桌子:

--TABLE products
create table products (
 reference char(5) not null  check ( reference like 'DT___'),
 designation char(50) not null,
 price numeric (9,2) not null,
 primary key(reference) );

-- TABLE interventions

create table interventions ( 
    nointerv integer not null ,
    dateinterv date not null,
    nameresponsable char(30) not null,
    nameinterv char(30) not null,
    time float not null check ( temps !=0 AND  temps between 0 and 8),
    nocustomers integer not null ,
    nofact integer not null ,
    primary key( nointerv),
    foreign key( noclient) references customers,
    foreign key (nofacture) references facts 
);

-- TABLE replacements

create table replacements (
    reference char(5) not null check ( reference like 'DT%'),
    nointerv integer not null,
    qtereplaced smallint,
    primary key ( reference, nointerv ),
    foreign key (reference) references products,
    foreign key(nointerv) references interventions(nointerv) 
);

-编辑: 这是我替换表中的一个选择 Table replacements

我们可以在这张图片中看到DT802产品被用于所有干预措施 谢谢;)

2 个答案:

答案 0 :(得分:0)

这将显示1行干预-产品。这是您期望的吗?

select interventions.nointerv, products.reference
from interventions
inner join replacements on interventions.nointerv = replacements.nointerv
inner join products on replacements.reference = products.reference;

这个吗?

select products.reference, products.designation
from interventions
inner join replacements on interventions.nointerv = replacements.nointerv
inner join products on replacements.reference = products.reference
group by products.reference, products.designation
having count(*) = (select count(*) from interventions);

答案 1 :(得分:0)

您的问题很难回答。如果我将其解释为nointervreplacements包含所有产品的所有reference,则:

select nointerv
from replacements r
group by nointerv
having count(distinct reference) = (select count(*) from products);