首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求一关于交集的sql语句,该如何解决

2012-02-01 
求一关于交集的sql语句表t1:idnamepropertyclass1小明标枪高三(1)2小明跳高高三(1)3大军标枪高三(1)4大军

求一关于交集的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) ')

热点排行