title: Compare values of a PHP array with a large table MySQL
body: <p>Ok, normally I would do something like this in PHP if I compared just few values:</p>
  
  <pre><code>// I want to get 1
  $a = array(1,2);
  $b = array(2);
  
  // It produces 1
  $result = array_diff($a, $b);
  </code></pre>
  
  <p>But in this case, <code>$b</code> is a large table in MySQL with millions of rows and retrieving it to PHP would be crazy, so my DBMS (MySQL) must do the work.</p>
  
  <p>How could I do something like:</p>
  
  <pre><code>-- I need treating $a as a column 
  SELECT $a NOT IN (SELECT id FROM b);
  </code></pre>
  
  <p><strong>Example</strong></p>
  
  <p><em>table B</em></p>
  
  <pre><code>|id|
   2
   3
   4
  </code></pre>
  
  <p><em>array A</em></p>
  
  <pre><code>array(1, 2, 3, 5);
  </code></pre>
  
  <p><em>Result</em></p>
  
  <p><code>A - B = A - INTERSECT(A,B) = [1, 5]</code></p>
  
  <p><strong>Example 2</strong></p>
  
  <p><a href="https://dba.stackexchange.com/a/37628/55440">This is</a> exactly what I need for SQL server. Any ideas to do something similar in MySQL?</p>
  
  <p>Note: A has about 5000 ids, so:</p>
  
  <pre><code>SELECT 1
  UNION SELECT 1
  UNION SELECT 1
  UNION SELECT 2
  UNION SELECT 5
  ...
  </code></pre>
  
  <p>...produces <code>#1064 - memory exhausted</code></p>