title: Dynamic MySQL Where Clause in Stored Procedure
body: <p>I have a question and maybe its simple (for you Gurus). </p>
  
  <p>I'm transposing my SQL Paging class from C# to a MySQL Stored Procedure. In my C# home-made object, the query is dynamically built based off a criteria. Example:</p>
  
  <pre><code>if(keywords is not null)
  { 
    whereClause += "WHERE description LIKE '%keywords%'"
  }
  if(price is not null)
  {
    whereClause += "AND price = '%price%'"
  }
  </code></pre>
  
  <p>....</p>
  
  <pre><code>string query = "SELECT col1, col2 FROM tblThreads " + whereClause
  </code></pre>
  
  <p>Now, my question is: How do I do a dynamic where clause in MySQL similar to this? Or rather, if they don't enter anything for those parameters, how would I tell MySQL in the Stored Procedure to skip those? IE:</p>
  
  <pre><code>SELECT col1, col2 FROM tblThreads
  </code></pre>
  
  <p>Would something like this work, if those parameters were null?</p>
  
  <pre><code>SELECT col1, col2 FROM tblThreads WHERE (IS NULL @keywords OR description like '%@keywords%'
  </code></pre>
  
  <p>??</p>
  
  <p>Thanks guys.</p>