title: MySQL UPDATE one table with values from another table if they're not equal
body: <p>I have two tables:</p>
  
  <pre><code>SELECT
      d.id,
      d.permission_lookup_id,
      d.permission_object_id,
      f.id,
      f.permission_lookup_id,
      f.permission_object_id
  FROM
      folders f
  LEFT JOIN
      documents d
      ON f.id = d.folder_id
  WHERE
      d.permission_lookup_id != f.permission_lookup_id OR
      d.permission_object_id != f.permission_object_id
  </code></pre>
  
  <p>In theory this should return nothing if all is well, but it isn't, I receive hundres of thousands of results (currently 407,343).  I need to update the <code>documents</code> table so that <code>permission_lookup_id</code> matches <code>folders</code>'s <code>permission_lookup_id</code> and likewise for <code>permission_object_id</code>, based off of <code>documents.parent_id</code> as the foreign key constraint.</p>
  
  <p>I tried the following query with little success:</p>
  
  <pre><code>UPDATE 
      folders f
  LEFT JOIN
      documents d
      ON f.id = d.folder_id
  SET
      d.permission_lookup_id = f.permission_lookup_id,
      d.permission_object_id = f.permission_object_id
  WHERE
      d.permission_lookup_id != f.permission_lookup_id OR
      d.permission_object_id != f.permission_object_id
  </code></pre>
  
  <p>After running that it tells me that 70,986 rows affected, and 407343 rows matched, yet I still have results from the first select query.</p>
  
  <p>EDIT: Both of the following queries return 0 rows:</p>
  
  <p><code>SELECT * FROM folders WHERE permission_lookup_id IS NULL OR permission_object_id IS NULL;</code></p>
  
  <p><code>SELECT * FROM documents WHERE permission_lookup_id IS NULL OR permission_object_id IS NULL;</code></p>
  
  <p>Same goes for blank values as well.</p>
  
  <p>EDIT 2:  There was an amateur mistake made in my original select/update queries.  In the WHERE clause, I had:</p>
  
  <pre><code>WHERE
      d.permission_lookup_id != f.permission_lookup_id OR
      d.permission_object_id != f.permission_lookup_id
  </code></pre>
  
  <p>Where I should have had </p>
  
  <pre><code>WHERE
      d.permission_lookup_id != f.permission_lookup_id OR
      d.permission_object_id != f.permission_object_id
  </code></pre>
  
  <p>That was causing me to receive false positives, and merely would have caused nothing more than to have the updates go through and update every column regardless of whether or not it needed to.  Sorry for the confusion.</p>