我已经被困在这个上一段时间了。这是一个实验室,这是最后一个问题。我结合上面的查询,找到了只提供计算机的供应商。任何帮助表示赞赏。
SELECT Name
FROM Tb_Supplier
WHERE Supp_ID IN
(SELECT Supp_ID
FROM Tb_Offers, Tb_Product
WHERE Tb_Offers.Prod_ID = Tb_Product.Prod_ID
AND (Tb_Product.Name = 'Computer'
OR Tb_Product.Name = 'Truck'))
AND Supp_ID NOT IN
(SELECT Supp_ID
FROM Tb_Offers, Tb_Product
WHERE Tb_Offers.Prod_ID = Tb_Product.Prod_ID
AND (Tb_Product.Name != 'Computer'
OR Tb_Product.Name != 'Truck'))
我只需要找到供应计算机和卡车的供应商,而不是其他任何东西。
答案 0 :(得分:0)
这为供应商提供了计算机和卡车,但没有别的
with dat
as
(
select 'Toronto' Supp_id,'Computer' thename union all
select 'Toronto' Supp_id,'Truck' thename union all
select 'Toronto' Supp_id,'Furniture' thename union all
select 'Ottawa' Supp_id,'Computer' thename union all
select 'Ottawa' Supp_id,'Truck' thename union all
select 'Montana' Supp_id,'Furniture' thename union all
select 'Idaho' Supp_id,'Computer' thename union all
select 'John' Supp_id,'Truck' thename union all
select 'John' Supp_id,'Furniture' thename union all
select 'Boris' Supp_id,'Computer' thename union all
select 'Boris' Supp_id,'Truck' thename union all
select 'Harold' Supp_id,'Furniture' thename union all
select 'Yelson' Supp_id,'Furniture' thename
)
select Supp_ID from dat where thename = 'Computer'
intersect
select Supp_ID from dat where thename = 'Truck'
except
select Supp_ID from dat where thename not in ('Truck','Computer')
答案 1 :(得分:0)
根据您提供的内容,这是一个解决方案。
SELECT tp.name,
ts.supp_ID
FROM Tb_Supplier ts
inner join tb_offers tb on tb.supp_id = ts.supp_id
inner join tb_prodcut tp on tp.prod_id = tb.prod_id
WHERE ts.supp_ID not in (select ts.supp_ID
from Tb_Supplier ts
inner join tb_offers tb on tb.supp_id = ts.supp_id
inner join tb_prodcut tp on tp.prod_id = tb.prod_id
where tp.product not in ('computers', 'truck')
)
答案 2 :(得分:0)
这需要“关系部门”
对聚合进行分组和比较解决了许多问题,这些问题需要“仅”通过数据进行单一传递,例如
select Supp_ID
from yourdata
group by Supp_id
having sum(case when product_name not in ('Truck','Computer') then 1 else 0 end) = 0
and min(product_name) = 'Computer'
and max(product_name) = 'Truck'
;
如果有任何其他产品,则SUM()将是<> 0 然后min和max必须等于2个产品
替代:
select Supp_ID
from yourdata
group by Supp_id
having sum(case when product_name not in ('Truck','Computer') then 1 else 0 end) = 0
and count(distinct product_name) = 2
;
如果有超过2种产品需要考虑,这种变体更容易修改。
请参阅:Divided We Stand: The SQL of Relational Division(Joe Celko)