On running the following MySQL (5.7.12) UPDATE statement, I get the error "Error Code: 1054. Unknown column ‘t1.col2’ in ‘where clause’".
How can I rephrase this query such that the columns of t1 are accessible in the subquery?
UPDATE MyFirstTable AS t1
INNER JOIN (
SELECT col1, col2
FROM MySecondTable
WHERE col2 > t1.col2
) AS t2
ON t1.col1 = t2.col1
INNER JOIN (
SELECT col1, col2
FROM MySecondTable
WHERE col2 < t1.col2
) AS t3
ON t1.col1 = t3.col1 AND t3.col2 = t2.col2
SET t2.col3 = t1.col3;
The objective here is to update MyFirstTable with rows from MySecondTable where, for a given col1 value, col2 was both less than and more than t1.col2 (on different rows, of course).
2
Answers
You don’t need a subquery here, simply make use of a multi-table update
Something like this should work:
Use
LATERAL
. See https://dev.mysql.com/doc/refman/8.0/en/lateral-derived-tables.htmlDemo:
As you discovered, a derived table cannot reference columns of other tables defined in the same FROM clause, which is standard SQL-92 behavior.
I made a couple of small changes to your query because it doesn’t make sense the way you wrote it. Your query sets t1.col3 to itself, which doesn’t accomplish anything. Also I added col3 to the derived table select-list, so it is available to the outer query.
The new feature in SQL:1999 was to make this legal if the derived table is preceded by the
LATERAL
keyword:With this change, the value of
col3
is copied from t2 to t1:Note that support in MySQL for the
LATERAL
feature was implemented in MySQL 8.0.14. If you use an older version of MySQL, you must upgrade to get this feature.