Skip to content
Advertisement

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. 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

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))

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.

Advertisement

Answer

You are attempting something called a lateral join — and MySQL does not support those. Because you want only one column, you can use a correlated subquery:

Note that I also changed the table aliases to be abbreviations for the table names — so the query is easier to read.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement