数据表结构如下:表t1
字段:
id text
1  T
1  T
2  T
2  F
3  T
3  T
4  T
4  F要求:通过一个sql查询,查出所有text字段值都是T的id,如例子的查询结果应该是
1和3

解决方案 »

  1.   

    select distinct id from tb where id not in (select id from tb where [text] <> 'T')
      

  2.   

    select id from tb
    group by id
    having(max(text)='T' and min(text)='T')
      

  3.   

    --> By dobear_0922(小熊) 2009-02-26 17:22:24
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([id] int,[text] varchar(1))
    insert [tb]
    select 1,'T' union all
    select 1,'T' union all
    select 2,'T' union all
    select 2,'F' union all
    select 3,'T' union all
    select 3,'T' union all
    select 4,'T' union all
    select 4,'F'select id from tb
    group by id
    having(max(text)='T' and min(text)='T')
    /*
    id
    -----------
    1
    3(2 行受影响)
    */drop table tb
      

  4.   

    create table t1 (id int,[text] varchar(1)) 
    insert into t1 values(1 , 'T') 
    insert into t1 values(1 , 'T') 
    insert into t1 values(2 , 'T') 
    insert into t1 values(2 , 'F') 
    insert into t1 values(3 , 'T') 
    insert into t1 values(3 , 'T') 
    insert into t1 values(4 , 'T') 
    insert into t1 values(4 , 'F') 
    go
    select distinct id from t1 where id not in (select id from t1 where [text] <> 'T')drop table t1/*id          
    ----------- 
    1
    3(所影响的行数为 2 行)
    */
      

  5.   


    select id from t1 where text ='t' group by id
      

  6.   

    select distinct id from tb where NOT EXISTS(select 'X' from tb where [text] <> 'T')
      

  7.   

    --name , sex 相同
    select m.* from tb m where exists (select 1 from
    (
      select name , sex  from  tb group by name , sex  having count(*) > 1
    ) n where n.name = m.name and n.sex = m.sex
    )/*
    name       sex        age         
    ---------- ---------- ----------- 
    A          M          17
    A          M          17(所影响的行数为 2 行)
    */
    发个例子看看
      

  8.   

       应该就是这样  
    select distinct id from t1 where id not in (select id from t1 where [text] <> 'T')
      

  9.   

    insert into Data 
    select 1  ,'T' union all
    select 1  ,'T' union all
    select 2  ,'T' union all
    select 2  ,'F' union all
    select 3  ,'T' union all
    select 3  ,'T' union all
    select 4  ,'T' union all
    select 4  ,'F'
    ---------------------------------
    select id from Data
    where [text]='T'
    group by [text],id
    having count(*)>1
    ---------------------------------
    id
    ---------
    1
    3
      

  10.   


    select distinct id from Data where id not in (select id from Data where [text] <> 'T')
    思路也很好。
      

  11.   

    sorry,10楼的答案欠考虑insert into Data 
    select 1  ,'T' union all
    select 1  ,'T' union all
    select 2  ,'T' union all
    select 2  ,'F' union all
    select 3  ,'T' union all
    select 3  ,'T' union all
    select 4  ,'T' union all
    select 4  ,'F'
    -------------------------------------------------
    select distinct id from Data 
    where id not in (select id from Data where [text] <> 'T')
    -------------------------------------------------
    id
    ---------
    1
    3