MySQL simple trigger can’t find the column in another table

I’m new with MySQL. I have this trigger:

DROP TRIGGER IF EXISTS `increment`;
TRIGGER `increment` 
AFTER INSERT ON `table1` 
FOR EACH ROW 
BEGIN 
   UPDATE table2 
   SET table2.mycolumn = table2.mycolumn+1 
   WHERE table2.id = table1.id; 
END 

It gives error:

Unknown column ‘table1.id’

Why? (The column exists). If I use this the trigger works fine:

WHERE table2.id = 1;

Help me understand this, please.

Answer

Use NEW or OLD to refer to the columns of the row that caused the trigger to be executed. But not columns of a table you subsequently change in a SQL statement in the body of the trigger.

Example:

TRIGGER `increment` 
AFTER INSERT ON `table1` 
FOR EACH ROW 
BEGIN 
   UPDATE table2 
   SET table2.mycolumn = table2.mycolumn+1 
   WHERE table2.id = NEW.id; 
END 

In this example, NEW.id refers to the id column of the current row inserted to the increment table.

There is no OLD version of the current row in an insert trigger, because it’s a new row.