如表有ID和状态两个字段
ID   State
1      1
1      1
2      1
2      0
2      1
3      1
3      0
4      1
5      0
6      1
6      1查找出同一ID状态都是1的所有ID(1,4,6)
先谢谢大家了。

解决方案 »

  1.   

    select distinct id where State=1
      

  2.   

    我这个估计效率会很低。
    不过如果实在没的用了, 就尝试下这个吧:select id from idAndState
    where  id=1 and state =1
    except
    select id from idAndState
    where id=1 and state<>1
      

  3.   

    select distinct id where State=1
      

  4.   

    declare @t table (id varchar(50),state varchar(50))
    insert into @t
    select '1','1' union all
    select '1','1' union all
    select '2','1' union all
    select '2','0' union all
    select '2','1' union all
    select '3','1' union all
    select '3','0' union all
    select '4','1' union all
    select '5','0' union all
    select '6','1' union all
    select '6','1'select distinct id from @t where id not in (select distinct id from @t where state<>'1')
      

  5.   

    5楼的代码有个bug。
    当存在 '6', null这样一行数据的时候, 
    6依然会被选出来。
      

  6.   

    select distinct id from a where id in
    (
    select id from (select id,state from a group by id, state) as a
    group by id having count(*)=1
    )
    and state=1
      

  7.   

    SELECT DISTINCT ID
    FROM         table
    WHERE     (ID NOT IN
                              (SELECT     ID
                                FROM          table
                                WHERE      (State = 0)))
      

  8.   

    declare @t table (id varchar(50),state varchar(50))
    insert into @t
    select '1','1' union all
    select '1','1' union all
    select '2','1' union all
    select '2','0' union all
    select '2','1' union all
    select '3','1' union all
    select '3','0' union all
    select '4','1' union all
    select '5','0' union all
    select '6','1' union all
    select '6',nullselect distinct id from @t where id not in (select distinct id from @t where isnull(state,'0')<>'1')那稍微改一下不就得了。你之前又没说,我哪里知道你还有这种需求
      

  9.   

    2楼问题相同。不知道这样行不行。
    select id from idAndState
    where  id=1 and state =1
    except
    select id from idAndState
    where id=1 and (state<>1 or state is null)
      

  10.   


    你在后面 加条件 限制一下即可select distinct id from @t where id not in (select distinct id from @t where state<>'1' and state is not null )
      

  11.   

    select * from (select ID,COUNT(*) as cnt from Table_Test where  state=1  group by ID) as temp where cnt>=2
      

  12.   

    12楼的sql好晕。
    不过用以下数据, sql结果是错的:id state
    1 1
    1 1
    2 1
    2 0
    2 1
    3 1
    1 NULL
      

  13.   


     declare @t table( [ID] int ,state int)
      insert into @t values(1   ,   1 )
      insert into @t values(1   ,   1 )
      insert into @t values(2   ,   1 )
      insert into @t values(2   ,   0 )
      insert into @t values(2   ,   1 )
      insert into @t values(3   ,   1 )
      insert into @t values(3   ,   0 )
      insert into @t values(4   ,   1 )
      insert into @t values(5   ,   0 )
      insert into @t values(6   ,   1 )
      insert into @t values(6   ,   1 )  select [ID] from @t group by [ID] having Count( distinct state) = 1 and avg(state) =1
      

  14.   


    select distinct id from #temp where state=1 and id not in(select id from #temp where state=0)
      

  15.   

    select distinct id from a where  id not in(select id from a where state=0)
      

  16.   

    select distinct [ID] from table1 where [ID] not in(select distinct [ID] from table1 where state != 1)
      

  17.   

    试试这个SELECT ID,STATE,COUN(ID) FROM TABLE GROUP BY ID,state HAVING COUNT(*)>=2 and state=1
      

  18.   

    18楼的HAVING COUNT(*)>=2 条件是在group by 分组后进行筛选的,所以感觉不对
      

  19.   

    select id from * where sate=1 group by id
      

  20.   

    declare @t table( [ID] int ,state int)
      insert into @t values(1   ,   1 )
      insert into @t values(1   ,   1 )  insert into @t values(2   ,   1 )
      insert into @t values(2   ,   0 )
      insert into @t values(2   ,   1 )  insert into @t values(3   ,   1 )
      insert into @t values(3   ,   0 )  insert into @t values(4   ,   1 )  insert into @t values(5   ,   0 )  insert into @t values(6   ,   1 )
      insert into @t values(6   ,   1 )
    select distinct ID from @t as a
    where not exists(select ID from @t  where ID=a.ID and isnull(state,'0')=0 )
      

  21.   

    select distinct id from #temp where state=1 and id not in(select id from #temp where state<>1)
      

  22.   

    declare @t table (id varchar(50),state varchar(50))
    insert into @t
    select '1','1' union all
    select '1','1' union all
    select '2','1' union all
    select '2','0' union all
    select '2','1' union all
    select '3','1' union all
    select '3','0' union all
    select '4','1' union all
    select '5','0' union all
    select '6','1' union all
    select '6','1'select distinct * from @t t where not exists(select 1 from @t where id=t.id and [state]=0)
      

  23.   

    SELECT  id, stat FROM idTable
    WHERE (stat = 1) 
        AND (id NOT IN (SELECT  id FROM idTable AS idTable_1  WHERE (stat = 0)))
      

  24.   

    select id  from 
    {
       select id from idAndState where state=1
    } t1 where t1.id not in(
    select id from
    {
       select id from idAndState where state=0
    }
    )
      

  25.   

    select distinct id from AA where id not in(
    select id from AA where state !=1 AND state is not null)
    应该没错了