【发布时间】:2018-09-13 16:01:11
【问题描述】:
我正在尝试将如下所示的外部查询中的列传递给 WHERE 子句中的内部查询,而 MySQL 不喜欢它。我不确定如何重写此查询以使其工作。
我收到的错误消息是 where 子句中的未知列 'y.DateShipped'
我想要做的是加入到内部表中的行,其 EffectiveDate 小于 DateShipped 并且也是内部联接中的最大 EffectiveDate(同一个组可以有多个行)不同的 EffectiveDate(s))
我很想知道如何让它工作或重写它以使其工作。我使用的是 MySQL 5.6,所以我没有可用的窗口函数,否则我认为这可以工作。
select
x.id,
y.id,
y.DateShipped
from Shipment y inner join
(select id, SourceId, DestinationId, SourcePPFContractId, EffectiveDate
from Relationship where EffectiveDate <= y.DateShipped order by
EffectiveDate desc limit 1) x
on x.DestinationId = y.DestinationCustomerId
and x.SourceId = y.OriginCustomerId
and x.SourcePPFContractId = y.OriginContractId;
【问题讨论】: