select a.*
from t_master a
inner join t_serv b
on a.id=b.id
where not exists(select 1 from t_serv where id=b.id and flag<>1)

解决方案 »

  1.   

    select id  ,name  from t_master where id in
     (select distinct id from t_serv where flag=1)
      

  2.   

    select id  ,name  from t_master where id in
     (select id from t_serv where flag=1 group by id having count(*)>1)
      

  3.   

    ---------
    select A.id  ,A.name  from t_master A inner join 
     (select id,count(*) num from t_serv where flag=1 group by id ) B
    on A.id=B.id 
    inner join (select id,count(*) num from t_serv group by id ) C
    on B.id=C.id and B.num=C.num
      

  4.   

    1): 把t_serv表中 flag <> 1的那些纪录找出来, 得到这些含有flag<>1的纪录的id.
    2): 再从t_serv表中把第一步的那些id过滤出去,剩下的就都是=1的id了
    3):从t_master表中找出含有第2步的id的纪录,就是结果了。
    select * from t_master where id in (  --(3)
        select  distinct id   from t_serv where id not in (   --(2)
            select id from t_serv where flag <> 1  -- (1)
        )
    )
      

  5.   

    楼上的分析不错,但是似乎考虑复杂了一点,直接两步就可以了。Select * from t_master 
    Where ID Not In (Select DIstinct ID from t_serv Where flag=0)这个比一楼的效率更高。
      

  6.   

    楼上的:
    那如果flag 不仅仅只有1,0这两个值, 怎幺办?
      

  7.   

    /*如果flag 不仅仅只有1,0这两个值*/
    可以考虑把flag转换成1,0来做!
      

  8.   

    回复人: edgethinking(向JAVA骨灰级进军) ( ) 信誉:100  2005-06-08 18:57:00  得分: 0  
     
     
       楼上的:
    那如果flag 不仅仅只有1,0这两个值, 怎幺办?------------------------------------
    小改动一下即可Select * from t_master 
    Where ID Not In (Select DIstinct ID from t_serv Where flag<>1)
     
      

  9.   

    select distinct t_master.* from t_master join t_serv on t_master.id=t_serv.id where flag=1
      

  10.   

    我想问一下,如果要在一个sql server 数据库里创建两个主键,该怎样做呢?
      

  11.   

    edgethinking(向JAVA骨灰级进军) 的方法条理比较清晰可以达到目的paoluo(一天到晚游泳的鱼) 的方法如果在主表里有而从表里没有的ID也会被选出来
      

  12.   

    select * from t_master join t_serv on t_master.id=t_serv.id where t_serv.flag=1
      

  13.   

    回复人: tripman(当牛粪遇上鲜花) ( ) 信誉:100  2005-06-09 11:15:00  得分: 0  
     
     
       edgethinking(向JAVA骨灰级进军) 的方法条理比较清晰可以达到目的paoluo(一天到晚游泳的鱼) 的方法如果在主表里有而从表里没有的ID也会被选出来
      
     
    ----------------------------------------
    哦,这一点的确没有考虑到。看你的例子,我以为主表有的ID细表中一定有的。那用edgethinking的吧。
      

  14.   

    --怎么这个问题有这么多要说的吗?先把我的答案贴出来.等阵子看看大家都在聊些什么.
    select * 
    from t_master
    where not exists(
    select*
    from t_serv
    where t_master.id=id
            and flag=0)
      

  15.   

    呵,filebat(Mark) ,你的语句的结果也和我的一样。会出现楼主说的这种情况,“主表里有而从表里没有的ID也会被选出来”。
      

  16.   

    --呵呵paoluo, 那咱们再加一个补丁,试试
    select * 
    from t_master
    where not exists(
    select*
    from t_serv
    where t_master.id=id
            and flag<>1)
    and exists( select* 
    from t_serv
    where t_master.id=id)
      

  17.   

    如下语比较简单而全面:
    select *
    from t_master
    where id not in (select distinct id form t_serv where flag<>1) 
    and id in (select distinct id from t_serv) //去掉有主表无从表的记录。
      

  18.   

    create table t_master
    (
      id int, name varchar(20)
    )create table t_serv
    (
      id int,sid int,flag int,re varchar(100)
    )goinsert t_master
    select 1,'A' union select 2,'B' union select 3,'C' union select 4,'D'insert t_serv
    select 1,1,1,'' union
    select 1,2,0,'' union
    select 2,1,1,'' union
    select 3,1,1,'' union
    select 3,2,1,'' union
    select 4,1,1,'' union
    select 4,2,0,'' union
    select 4,3,1,''select distinct A.id,(select name from t_master where id=A.id) as name 
    from t_serv A 
    where A.id not in (select distinct id from t_serv where flag=0)
    --删除测试数据
    drop table t_master
    drop table t_serv
    -- 结果
    2 B
    3 C
      

  19.   

    谢谢大家关注!vivianfdlpw() 的还是有问题以上都有几位给出了正确语句,关键是把主表里有而从表里没有的ID排除就OK我也总结了一条:select distinct a.* 
    from t_master a,t_serv b
    where not exists(select 1 from t_serv where id=a.id and  flag<>1) and a.id=b.id