find a row based on serverl creiteria
<p>I have a table as follows:</p>

<pre><code>id  var status warehouse
1    x    True    1
2    x    False   1  
3    y    False   1  
</code></pre>

<p>I want to write a query where I give it <code>var</code> and it returns me only one row as follows.
if it has status=True it will return this row first, if not it will return any status. if none found it will return nothing.</p>

<p>For example
for <code>x</code> it will return <code>id=1</code>   (2 is wrong!)
for <code>y</code> it will return <code>id=3</code> 
for <code>z</code> it will return nothing.</p>

<p>I know I can do it in a function with 2 queries like:</p>

<pre><code>select id into choose_id
from tab
where var=given_input and and status
limit 1;

IF choose_id IS NULL THEN
   select id into choose_id
   from tab
   where var=given_input 
   limit 1;
END IF;

continue work with choose_id
</code></pre>

<p>where <code>given_input</code> is <code>'x'</code> or <code>'z'</code> or <code>'y'</code> </p>

<p>is it doable in a single query without <code>IF</code> statment?</p>