gpt4 book ai didi

mysql - 仅从两个实体中获取匹配的记录

转载 作者:行者123 更新时间:2023-11-29 15:14:38 25 4
gpt4 key购买 nike

请帮我进行 MYSQL 查询。

实体名称:OrderItem属性:orderId(PK)、orderItemSeqId(PK)、productId

实体名称:ProductFacility属性:facilityId(PK)、productId(PK)、inventoryCount(整数)

**OrderId  | orderItemSeqId | productId**

OID1 | 0001 | 10000

OID1 | 0002 | 10001

OID1 | 0003 | 10002


**FacilityId | ProductId | InventoryCount**

FC_1 | 10000 | 12

FC_1 | 10001 | 5

FC_1 | 10002 | 7

FC_2 | 10001 | 1

FC_2 | 10002 | 6

FC_3 | 10002 | 7

在这里,我想获取具有所有可供订购产品的设施 (FC_1)。

我不想要包含部分产品的设施记录(例如设施 FC_3 只有订单 OID1 中的一种 (10002) 产品)

我只想要包含订单中所有产品的facilityId 记录(例如 OID1)

重要点:此 SQL 将在数百万条记录上执行。

最佳答案

试试这个:

select * 
from ProductFacility f1
where (select count(distinct InventoryCount) cnt_fac
from ProductFacility f2
where f2.FacilityId = f1.FacilityId
and InventoryCount > 0
group by FacilityId ) = (select count(distinct productId)
from OrderItem );

这是一个DEMO

关于mysql - 仅从两个实体中获取匹配的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59817812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com