title: using conditional where clauses in mysql select statement
body: <p>There is a stored procedure <code>sp_select_from_persons</code> in MySql.  And there are 3 parameters inside this procedure. <code>age, class, group</code>. These parameter values can be -1 or another value. And these parameters will be used in <code>where</code> clause.<br />
  Now I want to write where clause like that, if any value of these parameter is another than <strong>-1</strong> then append them into <code>where</code> clause. For example:<br />
  if age = -1, class = 7 and group = 5 then select query will be:</p>
  <pre><code>SELECT * FROM persons p WHERE p.class = class AND p.group = group;  
  </code></pre>
  <p>or if age = 25, class = -1 and group = -1 then:</p>
  <pre><code>SELECT * FROM persons p WHERE p.age = 25;
  </code></pre>
  <p>and etc.</p>
  <p>I don't want to use emnedded IF statements like below:</p>
  <pre><code>IF age &gt; 0 THEN 
    IF class &gt; 0 THEN
       IF group &gt; 0 THEN
       SELECT * FROM persons p WHERE p.age = age AND p.class = class AND p.group = group;
       END IF
       ELSE ....... etc
  </code></pre>
  <p>Is there a better way to do this?</p>