php - MySQL : select column values which do not repeat with both possible values 1 and -1 -
i have db table follows
id | branch | allot 1 | comp | -1 2 | | -1 3 | comp | -1 1 | comp | 1
where allot=-1
means seat allotted , allot=1
means seat cancelled
i want mysql query return id=2,3 only
not id=1 has cancelled seat later on.
your question says "later on". cannot done data format, because sql tables represent unordered sets. and, have no column specifies ordering.
if want -1
values , exclude 1
, there several ways. 1 use not exists
:
select t.* dbtable t allot = -1 , not exists (select 1 dbtable t2 t2.id = t.id , t2.allot = 1 );
if later discover column have ordering records (such createdat
), can modify to:
select t.* dbtable t allot = -1 , not exists (select 1 dbtable t2 t2.id = t.id , t2.allot = 1 , t2.createdat > t.createdat );
Comments
Post a Comment