表结构,companyid是公司名,productid是产品号,说明每个公司生产多少种产品。companyid   productid  
A                1
A                2 
B                1
B                2 
B                3
C                1 
D                1
D                2 
D                5
要求:取出所有生产的产品包含公司A所生产产品的所有公司名。
例如,公司A生产1,2,那么产品中至少包含1,2(可以更多)的公司名被选出,即A,B,D求一句实现的sql语句。

解决方案 »

  1.   

    select companyid from T A 
    where not exists(select 1 from T where companyid='A' and productid not in (select productid from T where companyid = A.companyid))
      

  2.   

    select distinct a.companyid  
    from 你的表 a 
    where a.productid in (select productid from 你的表 b where b.companyid='A' )
      

  3.   

    select  a.companyid  
    from 你的表 a 
    where exists  (select 1 from 你的表 b where b.companyid='A' and a.productid =b.productid  )
    group by a.companyid 
    having count(a.productid)>=2
      

  4.   

    select companyid from T A 
    where not exists
    (select 1 from T where companyid='A' and productid not in 
    (select productid from T where companyid = A.companyid))
      

  5.   

    select distinct companyid from tablename a inner join  (select  productid  from tablename where companyid='A') b on a.productid  =b.productid
      

  6.   

    create table com (id varchar(4),pro int)
    insert into com 
    select 'A',1
    union all
    select 'A',2
    union all
    select 'B',1
    union all
    select 'B',2
    union all
    select 'B',3
    union all
    select 'C',1
    union all
    select 'D',1
    union all
    select 'D',2
    union all
    select 'D',5
    select [id] from com a where not exists(select 1 from com where [id]='A' and pro not in
    (select pro from com where [id]=a.[id])) group by [id]
      

  7.   

    declare  @tab table (companyid varchar(10),productid varchar(10))
    Insert Into @tab Values('A'      ,          1)
    Insert Into @tab Values('A'         ,       2 )
    Insert Into @tab Values('B'         ,       1)
    Insert Into @tab Values('B'       ,         2 )
    Insert Into @tab Values('B'        ,        3)
    Insert Into @tab Values('C'         ,       1 )
    Insert Into @tab Values('D'         ,       1)
    Insert Into @tab Values('D'        ,        2 )
    Insert Into @tab Values('D'         ,       5)select companyid from @tab A 
    where not exists(select 1 from @tab where companyid='A' and productid not in (select productid from @tab where companyid = A.companyid))
    Group By companyid
      

  8.   

    统计每个公司生产多少种产品
    select companyid,count(productid) from tablename group by companyid
      

  9.   

    declare @t table (companyid varchar(20),productid int)
    insert @t select 'A', 1
    union all select 'A', 2 
    union all select 'A', 5
    union all select 'B', 1
    union all select 'B', 2 
    union all select 'B', 3
    union all select 'C', 1 
    union all select 'D', 1
    union all select 'D', 2 
    union all select 'D', 5
    select companyid from (select a.companyid,a.productid from @t a Inner Join (select productid from @t where companyid='A') b on a.productid=b.productid) dd
    group by companyid having count(companyid)=(select count(productid) productid from @t where companyid='A')
      

  10.   

    DROP TABLE #T
    CREATE TABLE #T
    (COMPANYID CHAR(1),
      PRODUCTID  INT
    )INSERT INTO #T SELECT 'A'  ,              1 UNION
    SELECT 'A'   ,             2  UNION
    SELECT 'B'  ,              1 UNION
    SELECT 'B'     ,           8  UNION
    SELECT 'B'      ,          3 UNION
    SELECT 'C'    ,            1 UNION
    SELECT 'D'     ,           1 UNION
    SELECT 'D'     ,           2 UNION
    SELECT 'D'         ,       5 
    SELECT COMPANYID,COUNT(1) AS N FROM #T WHERE PRODUCTID IN(SELECT PRODUCTID FROM #T WHERE COMPANYID='A')
    GROUP BY COMPANYID HAVING COUNT(1)>=2
      

  11.   

    这是上面的运行结果COMPANYID N
    ------------------
    A 2
    B 2
    D 2
      

  12.   

    select 
      companyid
    from 
      表
    where
      productid in (select 
                      productid 
                    from 
                      表
                    where
                      companyid ='A')
    group by 
      companyid
      

  13.   

    create table aaa
    (
    companyid varchar(2),
    productid int
    )
    goinsert aaa values('a',1)
    insert aaa values('a',2)
    insert aaa values('b',1)
    insert aaa values('b',2)
    insert aaa values('b',3)
    insert aaa values('c',1)
    insert aaa values('d',1)
    insert aaa values('d',2)
    insert aaa values('d',3)
    go/*从生产“产品1”的结果里筛选出能够生产“产品2”的结果*/
    select companyid from aaa where productid=1 and companyid in (select companyid from aaa where productid=2)/*运行结果如下*/
    companyid 
    --------- 
    a
    b
    d