有两张表:公司表company(id,name...),合同表hetong(id,title,companyid,type...)
一个公司可能有多长合同,type为1-10登记
例如:company表
                id  name
                1   a公司
                2   b公司
  
       hetong表
               id    title   companyid   type
               1     1合同     1           1
               2     2合同     1           2 
               3     3合同     1           7
               4     4合同     1           8
               5     5合同     2           7
               6     6合同     2           8
               7     7合同     2           9
               8     8合同     2           10
         
现在我像要赛选出:所有的type>6的 公司    
如果:select a.id ,distinct a.name from company a,hetong b where a.id=b.companyid and b.type>6
那会把a公司也赛选出来,这个sql怎么写啊
帮忙啊·····

解决方案 »

  1.   

    type大于6肯定会把A公司选出来啊。你要把a公司去掉,那加个条件就可以了!
      

  2.   

    select distinct  a.id ,a.name from company a,hetong b where a.id=b.companyid and b.type>6 是这样写,因为
    3    3合同    1          7
    4    4合同    1          8 
    a公司也有type大于6的,肯定会有a公司的。
      

  3.   

    我要赛选出:主要查询公司,这个公司只要有一个合同的type小于6 都不要
    不能用嵌套表,因为mysql版本小于4
      

  4.   

    已解决:
    SELECT * 
    FROM hetong
    LEFT JOIN company ON company.id = b.companyid
    GROUP BY test.name
    HAVING min( type ) >6
      

  5.   

    SELECT * FROM hetong LEFT JOIN company ON hetong.id = company .companyid where type  >6