简体   繁体   English

MYSQL:错误代码:1054。“where 子句”中的未知列

[英]MYSQL: Error Code: 1054. Unknown column in 'where clause'

I'm trying to pass a column from the outer query as shown below to the inner query in the WHERE clause and MySQL does not like it.我试图将一列从外部查询(如下所示)传递到 WHERE 子句中的内部查询,但 MySQL 不喜欢它。 I'm unsure how to rewrite this query to make it work.我不确定如何重写此查询以使其工作。

The error message I am getting is Unknown column 'y.DateShipped' in where clause我收到的错误消息是where 子句中的 Unknown column 'y.DateShipped'

What I am trying to do is to join to the row in the inner table with an EffectiveDate that is less than the DateShipped and also is the max EffectiveDate in the inner join (there can be multiple rows for the same group by with different EffectiveDate(s))我想要做的是连接到内表中的行,其 EffectiveDate 小于 DateShipped 并且也是内连接中的最大 EffectiveDate(同一组可以有多个行,具有不同的 EffectiveDate( s))

I would love to know how to get this working or rewrite it so that it will work.我很想知道如何让它工作或重写它以使其工作。 I am using MySQL 5.6, so I don't have window functions available otherwise I think that could work.我使用的是 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; 

The inner select (from Relationship) is executed first and then merged with the first select.内部选择(来自关系)首先执行,然后与第一个选择合并。 That's why it doesn't work.这就是它不起作用的原因。 You should move the DateShipped to the where clause of the first select:您应该将 DateShipped 移动到第一个选择的 where 子句:

select 
    x.id,
    y.id,
    y.DateShipped 
from Shipment y inner join
    (select id, SourceId, DestinationId, SourcePPFContractId, EffectiveDate 
    from Relationship order by 
    EffectiveDate desc limit 1) x 
on x.DestinationId = y.DestinationCustomerId 
and x.SourceId = y.OriginCustomerId 
and x.SourcePPFContractId = y.OriginContractId
and x.EffectiveDate <= y.DateShipped; 

you would need to list the shipment table in the sub query to be able to call it properly try:您需要在子查询中列出发货表才能正确调用它,请尝试:

select 
    x.id,
    y.id,
    y.DateShipped 
from Shipment y inner join
    (select id, SourceId, DestinationId, SourcePPFContractId, EffectiveDate 
    from Relationship, Shipment where EffectiveDate <= Shipment.DateShipped order by 
    EffectiveDate desc limit 1) x 
on x.DestinationId = y.DestinationCustomerId 
and x.SourceId = y.OriginCustomerId 
and x.SourcePPFContractId = y.OriginContractId; 

You are attempting something called a lateral join -- and MySQL does not support those.您正在尝试一种叫做横向连接的东西——而 MySQL 不支持这些。 Because you want only one column, you can use a correlated subquery:因为您只需要一列,所以可以使用相关子查询:

select (select r.id 
        from Relationship r
        where r.DestinationId = s.DestinationCustomerId and
              r.SourceId = s.OriginCustomerId and
              r.SourcePPFContractId = s.OriginContractId and
              r.EffectiveDate <= s.DateShipped
        order by r.EffectiveDate desc
        limit 1
       ) as x_id,
       s.id, s.DateShipped 
from Shipment s ;

Note that I also changed the table aliases to be abbreviations for the table names -- so the query is easier to read.请注意,我还将表别名更改为表名称的缩写——因此查询更易于阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM