主表A:有字段 gid,
              adddate,(添加时间)
              objNum,  (对象数量)
子表B:有字段 gid,    (与主表A关联)
              obj1,  (对象)
        同一个gid可能有多个对象现要根据 添加时间、对象数量、对象 查询数据库中是否有完全匹配的数据例: 表A数据:  1,2010,2
                2,2010,3
     表B数据:  1,obj1
                1,obj2
                2,obj1
                2,obj2
                2,obj3
如果要查询 时间为2010,对象数量为2,对象为obj1,obj2 则结果为存在
如果要查询 时间为2010,对象数量为2,对象为obj1,obj3 则结果为不存在

解决方案 »

  1.   

    没看懂 
    这题目跟GID 什么关系
      

  2.   

    GID 只是一个字段名称而已
      

  3.   

    declare @date varchar(10),@num int,@duixiang varchar(100)
    set @date='2010'
    set @num=2
    set @duixiang='obj1,obj2'
    if exists(
    select b.gid
    from  a join b on a.gid=b.gid
    where a.adddate=@date and a.objNum=@num 
    and CHARINDEX(b.obj1,@duixiang)>0
    group by b.gid
    having COUNT(b.obj1)=LEN(@duixiang)-LEN(REPLACE(@duixiang,',',''))+1)print '存在'
    else 
     print '不存在'
      

  4.   

    /*----------------------------------------------------------------
    -- Author  :feixianxxx(poofly)
    -- Version:
    --      Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86) 
    Mar 29 2009 10:27:29 
    Copyright (c) 1988-2008 Microsoft Corporation
    Enterprise Evaluation Edition on Windows NT 6.1 <X86> (Build 7600: )

    ----------------------------------------------------------------*/
    if OBJECT_ID('a') is not null
    drop table a
    go
    create table a (gid int, adddate varchar(10) , objNum int)
    insert a select
    1,'2010',2 union select 
    2,'2010',3
    go
    if OBJECT_ID('b') is not null
    drop table b
    go
    create table b (gid int, obj1 varchar(10))
    insert b select
    1,'obj1' union select 
    1,'obj2' union select 
    2,'obj1' union select 
    2,'obj2' union select 
    2,'obj3' 
    go
    declare @date varchar(10),@num int,@duixiang varchar(100)
    set @date='2010'
    set @num=2
    set @duixiang='obj1,obj3'
    select case when 
    (select b.gid
    from  a join b on a.gid=b.gid
    where a.adddate=@date and a.objNum=@num 
    and CHARINDEX(b.obj1,@duixiang)>0
    group by b.gid
    having COUNT(b.obj1)=LEN(@duixiang)-LEN(REPLACE(@duixiang,',',''))+1)>0
    then '存在'
    else  '不存在' end
    /*------
    不存在*/
      

  5.   

     COUNT(b.obj1)=LEN(@duixiang)-LEN(REPLACE(@duixiang,',',''))+1)4F,这句厉害