title: MySQL: Combining two repetitive queries
body: <p>I have this in info in a table where sometime the <code>pd_id</code> is <code>NULL</code></p>
  
  <pre><code>doc_id  doc_title   doc_order   pg_id 
  14      a.zip       1           NULL    
  15      b.zip       2           NULL    
  12      c.zip       1           16  
  13      d.zip       2           16      
  3       f.doc       3           16      
  4       g.doc       4           16  
  </code></pre>
  
  <p>When I want to query the item with the pg_id 16, I do this,</p>
  
  <pre><code>SELECT * 
  FROM root_documents 
  WHERE root_documents.pg_id = '16'
  </code></pre>
  
  <p>While when I want query the item without any pd_id, I do this,</p>
  
  <pre><code>SELECT * 
  FROM root_documents 
  WHERE root_documents.pg_id IS NULL
  </code></pre>
  
  <p>I find two queries are repetitive, so I try to this,</p>
  
  <pre><code>SELECT * 
  FROM root_documents 
  WHERE (root_documents.pg_id = ? OR root_documents.pg_id IS NULL)
  </code></pre>
  
  <p>So when I want to get this result only when I query the item with pg_id 16,</p>
  
  <pre><code>doc_id  doc_title   doc_order   pg_id 
  12      c.zip       1           16  
  13      d.zip       2           16      
  3       f.doc       3           16      
  4       g.doc       4           16
  </code></pre>
  
  <p>But I get all of them intead!</p>
  
  <pre><code>doc_id  doc_title   doc_order   pg_id 
  14      a.zip       1           NULL    
  15      b.zip       2           NULL    
  12      c.zip       1           16  
  13      d.zip       2           16      
  3       f.doc       3           16      
  4       g.doc       4           16
  </code></pre>
  
  <p>How can I fix this query,</p>
  
  <pre><code>SELECT * 
  FROM root_documents 
  WHERE (root_documents.pg_id = ? OR root_documents.pg_id IS NULL)
  </code></pre>
  
  <p>Or do I have to repeat the query like I usually do?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>This answer looks strange to me when I have a parameter to be passed into the placeholder:</p>
  
  <p><code>SELECT * 
  FROM root_documents 
  WHERE (root_documents.pg_id = '16' OR (root_documents.pg_id IS NULL AND '16' IS NULL))</code></p>
  
  <p>and when without a parameter,</p>
  
  <p><code>SELECT * 
  FROM root_documents 
  WHERE (root_documents.pg_id = NULL OR (root_documents.pg_id IS NULL AND NULL IS NULL))</code></p>
  
  <p>Note that I pass <code>NULL</code> into the <code>placeholder</code> when no value is returned.</p>