日期:2014-05-18 浏览次数:20697 次
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([姓名] nvarchar(2),[科目] nvarchar(2))
Insert #T
select N'张三',N'英语' union all
select N'张三',N'数学' union all
select N'张三',N'语文' union all
select N'李四',N'英语' union all
select N'李四',N'数学' union all
select N'王五',N'英语'
Go
Select *
from #T t
where exists(select 1
from #t
where [姓名]=t.[姓名] and [科目] =N'英语')
and exists(select 1
from #t
where [姓名]=t.[姓名] and [科目] =N'数学')
and exists(select 1
from #t
where [姓名]=t.[姓名] and [科目] =N'语文')
/*
姓名 科目
---- ----
张三 英语
张三 数学
张三 语文
(3 row(s) affected)
*/