有两张表:公司表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怎么写啊
帮忙啊·····
一个公司可能有多长合同,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怎么写啊
帮忙啊·····
3 3合同 1 7
4 4合同 1 8
a公司也有type大于6的,肯定会有a公司的。
不能用嵌套表,因为mysql版本小于4
SELECT *
FROM hetong
LEFT JOIN company ON company.id = b.companyid
GROUP BY test.name
HAVING min( type ) >6