zoukankan      html  css  js  c++  java
  • [SQL]不知道1

    表结构,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语句。

    方案一

    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 

    方案二

    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))
  • 相关阅读:
    12.10
    4.06Android使用EditText小技巧汇总
    4.05
    4.04Android学习
    4.03Android学习
    4.02Android学习
    4.01Android学习
    3.31构建之法读后感3
    3.30Android学习
    3.29Android学习
  • 原文地址:https://www.cnblogs.com/beeone/p/3622296.html
Copyright © 2011-2022 走看看