日期:2014-05-18  浏览次数:20386 次

求一关于交集的sql语句
表t1:
id         name         property         class
1           小明         标枪               高三(1)
2           小明         跳高               高三(1)
3           大军         标枪               高三(1)
4           大军         铅球               高三(1)
5           小明         标枪               高三(7)
6           小明         跳高               高三(7)
7           大军         标枪               高三(7)
8           大军         铅球               高三(7)

我想得到  
高三(1)班的   小明         和
高三(7)班的   大军
他们两人参加相同的项目对应的ID      
结果应该是   1   ,   7       的
其实在需求的业务来说,
我想知道一些人他们共同参与的项目,
所以人数是不定的,而名字也有可能重复

想了很久,都搞不了,请问高手指点一下。
不明题意的话,就留一下言啊   万分感激

------解决方案--------------------
select a.id from sport a
join
(
select property from
(
select id,property from sport d where name = '小明 ' and class= '高三(1) '

union all

select id,property from sport where name = '大军 ' and class= '高三(7) '
)as b

group by b.property having count(*)> 1

)c
on a.property = c.property and (a.name = '小明 ' and a.class= '高三(1) '
or a.name = '大军 ' and a.class= '高三(7) ')