2010-07-24 3 views
0

의 값에 따라 다음으로 돌아 가기 : 내가 될 것이다 질의를 필요로SQL 쿼리 나는 유사한 테이블이 특정 행이 열

cycle | id | name 
------|-------|------ 
0  | 0012 | bob 
s  | 0012 | bob 
1  | 0012 | bob 
0  | 6208 | sally 
1  | 6208 | sally 
0  | 3276 | jane 
s  | 3276 | jane 
1  | 3276 | jane 
0  | 8736 | harry 
s  | 8736 | harry 

을에서 :

cycle | id | name 
------|-------|------ 
s  | 0012 | bob 
0  | 6208 | sally 
s  | 3276 | jane 
s  | 8736 | harry 

그래서만을 반환 특정 'id'에 대해 's'값이없는 경우가 아니면 'cycle'값이 '0'인 행을 반환해야합니다.

답변

0
SELCET cycle, id, name from tbl1 as t1 where cycle='s' or 
(cycle=0 and (select count(cycle) from tbl1 as t2 where t2.id = t1.id and t2.cycle='s') <= 0) 
group by t1.cycle,t1.id;