title: how should define an optional constraint in a query in mysql
body: <p>I have a simple question. I want to write a Query on MySql. So I wrote this store procedure to get some thing:</p>
  
  <pre><code>CREATE DEFINER=`root`@`%` PROCEDURE `attributes_fetchall`(p_origin_id bigint, p_status char(1))
  begin
  select * from attributes where status = p_status and origin_id = p_origin_id;
  end
  </code></pre>
  
  <p>but I want to get all rows with <code>status=p_status</code> when <code>p_origin_id = null</code>. I mean I can get this query when <code>origin_id = null</code> :</p>
  
  <pre><code>select * from attributes where status = p_status
  </code></pre>
  
  <p>and when origin_id != null return this query:</p>
  
  <pre><code>select * from attributes where status = p_status and origin_id = p_origin_id;
  </code></pre>
  
  <p>How should I write this store procedure that can handle this 2 in a one query?</p>