查询真的很烦我,我无法理解它

Query is really annoying me I can't get my head around it

我是 SQL 的新手,我有以下 erd:

我要执行的查询是: 可以使用多少车辆来服务货物 x?|

有什么想法吗?? 我有:

select count(VehicleType) from Vehicle where in
 (select VEHICLETYPE, SHIPMENT_TYPE from can_carry
 where SHIPMENT_TYPE in
 (select SHIPMENT_TYPE from Shipment where ContractNo = x and ShipmentNo = x  );

但我觉得不对

您没有处于第一种情况的 VEHICLETYPE,还缺少一个 )。 所以这是有效的 SQL:

select count(VehicleType) from Vehicle where VEHICLETYPE in
(select VEHICLETYPE, SHIPMENT_TYPE from can_carry
 where SHIPMENT_TYPE in
 (select SHIPMENT_TYPE from Shipment where ContractNo = x and ShipmentNo = x))

但是这个 SQL 是 PERFKILLER! :) 我的意思是你选择了 3 组结果!所以这会有一个可怕的执行计划。 您可以使用 JOIN. More about execution plans here.

编写更好的查询