我必须显示包含来自同一生产商的至少三种产品的订单 但它会返回所有订单代码而不是我要求的订单代码。显然我的代码是错误的。 任何想法如何解决它?我应该尝试不同于计数的东西吗?
create view Orders as
select order_code, Consists.product_code, Offer.producer_code
from Consists, Producers, Offer, Product
where Consists.product_code = Product.product_code
and Product.product_code = Offer.product_code
and Offer.producer_code = Producers.producer_code
select order_code
from Order_a
where exists (select Orders.order_code, Orders.producer_code
from Orders, Offer
where Orders.product_code = Offer.product_code
and Orders.producer_code = Offer.producer_code
group by Orders.producer_code, Orders.product_code
having count(Offer.product_code) >=3 )
答案 0 :(得分:2)
您需要将内部查询与外部查询相关联。子查询还需要通过order_code
和producer_code
:
select order_code
from Order_a
where order_code in (select Orders.order_code
from Orders join
Offer
on Orders.product_code = Offer.product_code and
Orders.producer_code = Offer.producer_code
group by Orders.order_code, Orders.producer_code
having count(Offer.product_code) >=3
);
我也想知道条件应该是having count(distinct offer.product_code) >= 3)
。
您的查询返回所有行,因为子查询始终生成结果,因此exists
为真。