Question

I have a table like:

create table myTab(
  id integer primary key,
  is_available boolean not null default true
);

I need to do a query that returns only the first encountered row that has is_available set to false.

Was it helpful?

Solution

something like

select *
from myTab
where not is_available
order by id asc
limit 1

OTHER TIPS

Try out this ..

select id,is_available from myTab
where is_available = false
order by id asc
limit 1

If you want row from last inserted then go with this ..

    select id,is_available from myTab
    where is_available = false
    order by id desc
    limit 1

Alternatively, you could use NOT EXISTS to find the first tuple, in most cases this is the fastest solution, too:

SELECT *
FROM myTab mt
WHERE mt.is_available = False
AND NOT EXISTS (
    SELECT *
    FROM myTab nx
    WHERE nx.is_available = False
    AND nx.id < mt.id
    );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top