主表 a
id name
1  s
2  w
3  n从表b
id 主表id name
1   1      s1
2   1      s3
3   2      s1
4   2      s3
5   3      s1要求是给定一个主表 id 查出和它拥有相同从表name的记录
如上:
给出id=1,得到
1 s
2 w
因为1 s 和 2 w 的从表name是相同的?

解决方案 »

  1.   

    select b.id,a.name 
    from 主表 a join 从表 b on a.id=b.主表id 
    where a.id=1
      

  2.   


    --> 测试数据: @a
    declare @a table (id int,name varchar(1))
    insert into @a
    select 1,'s' union all
    select 2,'w' union all
    select 3,'n'
    --> 测试数据: @b
    declare @b table (id int,主表id int,name varchar(2))
    insert into @b
    select 1,1,'s1' union all
    select 2,1,'s3' union all
    select 3,2,'s1' union all
    select 4,2,'s3' union all
    select 5,3,'s1'SELECT * FROM @a A 
    WHERE NOT EXISTS

    SELECT * FROM @b B WHERE 主表id=1 
    AND 
    NOT EXISTS 
    (SELECT * FROM @b C WHERE C.name=B.name AND C.主表id=A.id)
    )
      

  3.   

    create table #a (id int ,name varchar(1))
    insert into #a select 1,'s' union all select 2,'w' union all select 3,'n'
    create table #b (id int ,bid int,name varchar(2))
    insert into #b select 1,1,'s1' union all select 2,1,'s3' union all select 3,2,'s1' 
    union all select 4,2,'s3' union all select 5,3,'s1' select b.id,a.name 
    from #a a join #b b on a.id=b.bid 
    where a.id=1
    select *from #a where id in(
    select b.id from #a a left join #b  b on a.id=b.bid and a.id='1')1 s 
    2 w 
      

  4.   

    if object_id('ta') is not null
    drop table ta
    go
    create table ta(id int,name varchar(50))
    insert into ta select 1,'s'
    insert into ta select 2,'w'
    insert into ta select 3,'n'
    if object_id('tb') is not null
    drop table tb
    go
    create table tb(id int,tbid int,name varchar(50))
    insert into tb select 1,1,'s1'
    insert into tb select 2,1,'s3'
    insert into tb select 3,2,'s1'
    insert into tb select 4,2,'s3'
    insert into tb select 5,3,'s1'
    select * from ta where id in(
    select tbid from tb t 
    where name in(select name from tb where tbid=1)
    group by tbid having count(1)=(select count(1) from tb where tbid=1))id name
    1 s
    2 w
      

  5.   

    你没有判断NAME是否一致,你只判断个数一样
      

  6.   

    3楼的有判断
    SELECT * FROM @a A 
    WHERE NOT EXISTS

    SELECT * FROM @b B WHERE 主表id=1 
    AND 
    NOT EXISTS 
    (SELECT * FROM @b C WHERE C.name=B.name AND C.主表id=A.id)  --这有判断NAME是否一致的问题
    )
      

  7.   


    if object_id('ta') is not null
    drop table ta
    go
    create table ta(id int,name varchar(50))
    insert into ta select 1,'s'
    insert into ta select 2,'w'
    insert into ta select 3,'n'
    if object_id('tb') is not null
    drop table tb
    go
    create table tb(id int,tbid int,name varchar(50))
    insert into tb select 1,1,'s1'
    insert into tb select 2,1,'s3'
    insert into tb select 3,2,'s1'
    insert into tb select 4,2,'s1'
    insert into tb select 5,3,'s1'
    select * from ta where id in(
    select tbid from tb t 
    where name in(select name from tb where tbid=1)
    group by tbid having count(1)=(select count(1) from tb where tbid=1))--结果
    /*
    id          name                                               
    ----------- -------------------------------------------------- 
    1           s
    2           w*//*
    1 2在TB表里NAMEI 不一样的吧,2是没有S3的吧。
    insert into tb select 1,1,'s1'
    insert into tb select 2,1,'s3'
    insert into tb select 3,2,'s1'
    insert into tb select 4,2,'s1'
    */
      

  8.   

    呵呵~~~
    wzy_love_sly 
    是你没看好题目还是是理解错了?
      

  9.   

    id 主表id name
    1  1      s1
    2  1      s3
    3  2      s1
    4  2      s3
    5  3      s1这不s3吗?
      

  10.   

    1  1      s1 
    2  1      s3 
    3  2      s1 
    4  2      s1 
    5  3      s1 如果数据是这样的呢?
    你那个查询就不行了吧
      

  11.   

    select * from ta a  where not exists(select 1 from tb where tbid=1 and not exists
      (select 1 from tb c where tb.name=c.name and a.id=c.tbid)) 其实也就是学习了张三所学习的全部课程的学生情况一个意思
      

  12.   

    1  1      s1 
    2  1      s3 
    3  2      s1 
    4  2      s1 
    5  3      s1  1  1      s1 
    2  1      s3 
    3  2      s1 
    4  2      s2 
    5  3      s1 像TB是这样两数据
    你的查找结果是一样的
      

  13.   


    with temp as (select dbo.string(b.id) as name from a,b where a.id=b.id and a.id =1 group by b.id)
    select * from a where a.id in(
    select b.id from b 
    group by b.id
    having dbo.string(b.id)=(select name from temp) 
    )alter function [dbo].[string](@id int)
    returns nvarchar(max)
    as
    begin
    declare @name nvarchar(10)
    declare @result nvarchar(max)
    SET @result=''
    declare aa cursor for select b.name from b where b.id=@id
    open aa
    fetch next from aa into @name
    while @@fetch_status=0
    begin
    set @result=@result+'-' + @name
    fetch next from aa into @name
    end
    close aa
    deallocate aa
    return @result
    end
      

  14.   

    if object_id('ta') is not null
    drop table ta
    go
    create table ta(id int,name varchar(50))
    insert into ta select 1,'s'
    insert into ta select 2,'w'
    insert into ta select 3,'n'
    if object_id('tb') is not null
    drop table tb
    go
    create table tb(id int,tbid int,name varchar(50))
    insert into tb select 1,1,'s1'
    insert into tb select 2,1,'s1'
    insert into tb select 3,1,'s3'
    insert into tb select 4,2,'s1'
    insert into tb select 5,2,'s1'
    insert into tb select 6,2,'s3'
    insert into tb select 7,3,'s1'
    insert into tb select 8,3,'s3'declare @tbid int
    set @tbid=1
    --与tbid=1完全相同
    select * from ta where id in(
    select tbid from (
    select name,count(1) as con from tb where tbid=@tbid group by name)t1 join (
    select tbid,name,count(1) as con from tb where tbid<>@tbid group by tbid,name)t2
    on t1.name=t2.name where t1.con=t2.con
    group by tbid having count(1)=(select count(distinct name) as con from tb where tbid=@tbid))id name
    2 w
      

  15.   

    重新测试了一下 这个方法不行 如果
    主表 a 
    id name 
    1  s 
    2  w 
    3  n 从表b 
    id 主表id name 
    1  1      s1 
    2  1      s3 
    3  2      s1 
    4  2      s3 
    5  3      s1 
    6  2      s2增加了 6  2  s2记录,1 s和2 w就不应该相等,不应该统计出来,但是这个方法没有过滤掉多余的,必须要从表name有且只有相同才查出!
      

  16.   


    select b.id,a.name 
    from 主表 a left join 从表 b on a.id=b.主表id 
    where a.id=1
      

  17.   

    select  a.*from a ,b where b.主表id='1'
    and
    b.id=a.id
      

  18.   

    --> 测试数据: @a
    declare @a table (id int,name varchar(1))
    insert into @a
    select 1,'s' union all
    select 2,'w' union all
    select 3,'n'
    --> 测试数据: @b
    declare @b table (id int,主表id int,name varchar(2))
    insert into @b
    select 1,1,'s1' union all
    select 2,1,'s3' union all
    select 3,2,'s1' union all
    select 4,2,'s3' union all
    select 5,3,'s1'
    select  t1.*from @a t1,@b t2 
    where 主表id='1'
    and t2.id=t1.id----1 s
    2 w
      

  19.   


    create  table a
    ( id int,
      name nvarchar(10)
    )
    create  table b
    ( id int,
      主表id nvarchar(10),
      name  nvarchar(10)
    )
    insert into a select 1,'s'
         union all select 2,'w'
         union all select 3,'n'
    insert into b select 1,1,'s1'
         union all select 2,1,'s3'
         union all select 3,2,'s1'
         union all select 4,2,'s3'
         union all select 5,3,'s1' 
    select b.id,a.name from b  join a 
    on b.id= a.id where  b.主表id=1drop table a
    drop table b
      

  20.   

    create table ta(id int,name varchar(50))
    insert into ta select 1,'s'
    insert into ta select 2,'w'
    insert into ta select 3,'n'
    if object_id('tb') is not null
    drop table tb
    go
    create table tb(id int,tbid int,name varchar(50))
    insert into tb select 1,1,'s1'
    insert into tb select 2,1,'s3'
    insert into tb select 3,2,'s1'
    insert into tb select 4,2,'s3'
    insert into tb select 5,3,'s1'
    insert into tb select 6,2,'s4'
    create proc GetFullMatchingName(@id int)
    as
     begin
       declare @findName nvarchar(100)--查找的字符串,
       declare @idStr nvarchar(100)--ID的分组字符串
       declare  @curId nvarchar(10)--当前的ID
       declare @curName  nvarchar(100) --当前ID对应的Name集
       declare @resultID nvarchar(100)   select @findName=isnull(@findName,'')+name from tb where tbid=@id  order by name--赋值   select @idstr=isnull(@idstr,'')+Convert(nvarchar,id)+',' from ta    while(@idstr is not null and @idstr<>'')
       begin
          set @curId=substring(@idstr,1,charindex(',',@idstr)-1)--截取当前ID
          set @idStr = stuff(@idstr,1,charindex(',',@idstr),'')--把当前ID去掉
          --
          set @curName=null
          select @curName = isnull(@curName,'')+name from tb where tbid=@curid order by name--当前ID对应的记录集 
           
          if(lower(@curName)=lower(@findName))
           set @resultID=isnull(@resultID,'')+@curID+','--保存结果集
           
       end
        set @resultID= stuff(@resultID,len(@resultID),1,'')
       
      --返回结果
       exec ('select * from ta where id in ('+@resultID+')') 
       
     end
    exec GetFullMatchingName 1
    id          name                                               
    ----------- -------------------------------------------------- 
    1           s(所影响的行数为 1 行)加点分吧,太辛苦了..呵呵
      

  21.   

    if object_id('ta') is not null
    drop table ta
    go
    create table ta(id int,name varchar(50))
    insert into ta select 1,'s'
    insert into ta select 2,'w'
    insert into ta select 3,'n'
    if object_id('tb') is not null
    drop table tb
    go
    create table tb(id int,tbid int,name varchar(50))
    insert into tb select 1,1,'s1'
    insert into tb select 2,1,'s3'
    insert into tb select 3,2,'s1'
    insert into tb select 4,2,'s2'
    insert into tb select 5,2,'s3'
    insert into tb select 6,3,'s1'
    insert into tb select 7,3,'s3'declare @tbid int
    set @tbid=1
    --与tbid=1完全相同
    select * from ta where id in(
    select tbid from tb where tbid in(
    select tbid from (
    select name,count(1) as con from tb where tbid=@tbid group by name)t1 join (
    select tbid,name,count(1) as con from tb where tbid<>@tbid group by tbid,name)t2
    on t1.name=t2.name where t1.con=t2.con
    group by tbid having count(1)=(select count(distinct name) as con from tb where tbid=@tbid))
    group by tbid having count(1)=(select count(1) from tb where tbid=@tbid)
    )id name
    3 n判断分组后的count和没分组的count