title: Mysql: Update table with select max of another table
body: <p>I have 2 different database in my MySql server.</p>
  
  <pre><code>First table DB1.contacts:
  
  id | name | code
  1  | foo  | 157
  2  | foo  | 95
  3  | foo  | 210
  
  Second table DB2.paperworks:
  
  id | name | contact_id
  1  | foo  | 0
  </code></pre>
  
  <p>I would like to update DB2.paperworks, set DB2.paperworks.contact_id = max(DB1.contacts.code) of DB1.contacts.contacts table where DB2.paperworks.name = DB1.contacts.name</p>
  
  <p>My desidered output should be:</p>
  
  <p>Second table after query DB2.paperworks:</p>
  
  <pre><code>id | name | contact_id
  1  | foo  | 210
  </code></pre>
  
  <p>This is my query:</p>
  
  <pre><code>UPDATE DB2.paperworks
  JOIN DB1.contacts
  ON DB2.paperworks.name = DB1.contacts.name
  SET DB2.paperworks.contact_id = DB1.contacts.code
  </code></pre>
  
  <p>I don't understand how to write che "MAX(code)" condition. Can you help me, please?</p>