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

我是新人,求一个解法
我有个表T
姓名   课程   成绩
张三   语文   60
李四   语文   50
张三   数学   70
李四   数学   80

我想查找所有课程成绩> =60的学生姓名
如果解

------解决方案--------------------
select distinct 姓名 from table where 成绩 > = 60
------解决方案--------------------
抱歉,更正一下:
declare @t table(姓名 varchar(10),课程 varchar(10),成绩 int)
insert @t
select '张三 ', '语文 ', 60 union all
select '李四 ', '语文 ', 50 union all
select '张三 ', '数学 ', 70 union all
select '李四 ', '数学 ', 80

----查询
SELECT 姓名 FROM(
select 姓名 from @t where 成绩 > = 60
group by 姓名,课程) AS t
GROUP BY 姓名 having count(*) = (select count(DISTINCT 课程) from @t)


/*结果
姓名
----------
张三
*/
------解决方案--------------------
create table test(姓名 varchar(10),课程 varchar(10),成绩 int)
insert test select '张三 ', '语文 ',60
union all select '李四 ', '语文 ',50
union all select '张三 ', '数学 ',70
union all select '李四 ', '数学 ',80

select distinct 姓名 from test a
where not exists(select 1 from test where 姓名=a.姓名 and 成绩 <60)

drop table test

姓名
----------
张三
------解决方案--------------------
后边有个a 是test这个表的别名!
就想select A AS 成绩一样的意思!·