문제

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.

도움이 되었습니까?

해결책

something like

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

다른 팁

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
    );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top