如何正确执行与具体要求的子查询

问题描述 投票:-1回答:1

随着指定的信息here

我需要显示在新加坡购买的出货量为项目的ShipperName,ShipmentID和DepartureDate。我还需要在为了做到这一点使用子查询。结果将需要由托运人名称在递减顺序递增顺序,然后DepartureDate排序。

进出口新的MySQL和子查询和连接是一个更难理解。只是想那么远,因为我没有在我试图澄清。

SELECT city FROM item WHERE = city LIKE 'Singapore' and ( 
SELECT ShipperName, ShipmentID, DepartureDate FROM shipment);
mysql
1个回答
0
投票

您可以通过连接三个表做

select ShipperName, ShipmentId, DepartureDate
from shipment s
inner join shipment_item si on si.shipmentId = s.shipmentId
inner join item i on i.ItemID = s.ItemID and City = 'Singapure' 
Order by ShipperName Asc, DepartureDate Desc

看来,你不需要子查询。但你也可以做到这一点

select ShipperName, ShipmentId, DepartureDate
from shipment s
where exists (
   select 1 from shipment_item si 
   inner join item i on i.ItemID = s.ItemID 
   where si.shipmentId = s.shipmentId and City = 'Singapure' 
)

通过ShipperName ASC,DepartureDate倒序

© www.soinside.com 2019 - 2024. All rights reserved.