请问下面这句话是怎么得到,是什么意思啊???

mycolin 2005-05-30 03:15:26
-- 建表及插入数据
create table StuTable
(
StudentID varchar(8),
CourseID varchar(10),
Achievement int
)

insert StuTable select 71101, 1, 80
union all select 71102, 4, 86
union all select 71103, 2, 45
union all select 71104, 2, 88
union all select 71102, 1, 90
union all select 71103, 1, 78
union all select 71103, 3, 34
union all select 71104, 3, 64
union all select 71104, 1, 56
union all select 71101, 4, 75
union all select 71101, 2, 76
union all select 71102, 3, 56
union all select 71103, 4, 57
union all select 71102, 2, 85
union all select 71101, 3, 97
union all select 71104, 4, 96

--选出每门学科成绩在前2位的记录,并按照 CourseID 和 Achievement 从高到低排序
Select * from StuTable A
Where Not Exists(
select 1 from StuTable
Where CourseID=A.CourseID And Achievement>A.Achievement
Having Count(*)>1
)
Order by CourseID,Achievement Desc
------------------------------------------------------------------------------------------
现在想知道上面的是怎么得到的啊??
select 1 from StuTable -- 这句得到了什么结果呢??请大家分析一下:
Where CourseID=A.CourseID And Achievement>A.Achievement
Having Count(*)>1







...全文
129 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mycolin 2005-05-31
  • 打赏
  • 举报
回复
wangdehao(找找找) :
哦,呵呵,不好意思,我可能看错了
wangdehao 2005-05-30
  • 打赏
  • 举报
回复
Select * from StuTable A
Where Exists(
select 1 from StuTable
Where CourseID=A.CourseID And Achievement>A.Achievement
Having Count(*)<=1
)
Order by CourseID,Achievement Desc

是错误的吗?我这测试的数据显示是正确的。高手和楼主能帮着看一下吗?
pewaloti 2005-05-30
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/4041/4041816.xml?temp=.3728754
xluzhong 2005-05-30
  • 打赏
  • 举报
回复
另外一种方法:(较好理解)
Select *
From StuTable a
Where StudentID in (Select Top 2 StudentID
From StuTable
Where CourseID=a.CourseID
Order by Achievement desc)
order by CourseID,Achievement desc
duanduan1122 2005-05-30
  • 打赏
  • 举报
回复
select 1 from StuTable -- 这句得到了什么结果呢??请大家分析一下:
Where CourseID=A.CourseID And Achievement>A.Achievement
Having Count(*)>1

这里的意思是:
从外查询拿到一条记录,到子查询,统计这条记录在整张表中选择的课程一样,且得分大于它的人数的总数,并且总的人数要大于1。
duanduan1122 2005-05-30
  • 打赏
  • 举报
回复
Select * from StuTable A
Where Not Exists(
select 1 from StuTable
Where CourseID=A.CourseID And Achievement>A.Achievement
Having Count(*)>1
)
Order by CourseID,Achievement Desc
/*工作原理是这样的:
1。子查询先执行,当执行到where的时候执行不下去了,因为
不知道a是谁,所以跳出子查询,到外查询,知道a是谁了,然后从a中拿出一条记录到
子查询,这时候子查询扫描整个stutable表,得到结果给外查询,这时候外查询得到一个结果。
给你举个例子吧:
1。子查询执行,执行到where时,不知道a是谁
2。跳到外查询,从外查询中取一条记录,也就是(71101, 1, 80)到子查询
3。子查询用这条记录查询,得到count(*)=1,返回给外查询
4。外查询这时候有了条件,执行,因为Not Exists为真满足条件,执行,得到71101, 1, 80
5。然后子查询在执行,然后在跳到外查询
6。以此类推,然后得到结果。
*/
xluzhong 2005-05-30
  • 打赏
  • 举报
回复
-- 这句得到了什么结果呢??请大家分析一下:
Select 1 from StuTable
Where CourseID=A.CourseID And Achievement>A.Achievement
Having Count(*)>1

HAVING 子句
指定组或聚合的搜索条件。HAVING 通常与 GROUP BY 子句一起使用。如果不使用 GROUP BY 子句,HAVING 的行为与 WHERE 子句一样。

语法
[HAVING <search_condition>]

参数
<search_condition>

指定组或聚合应满足的搜索条件。当 HAVING 与 GROUP BY ALL 一起使用时,HAVING 子句替代 ALL。有关更多信息,请参见搜索条件。

在 HAVING 子句中不能使用 text、image 和 ntext 数据类型。

说明 在 SELECT 语句中使用 HAVING 子句不影响 CUBE 运算符分组结果集和返回汇总聚合行的方式。


mycolin 2005-05-30
  • 打赏
  • 举报
回复
还是不清楚,而且 wangdehao(找找找) 的修改建议是有错误的

期待高手指点迷津!!!!!!!!!!1
wangdehao 2005-05-30
  • 打赏
  • 举报
回复
select 1 from StuTable -----如果满足where条件的话显示1
Where CourseID=A.CourseID And Achievement>A.Achievement Having Count(*)>1 ---满足功课号(CourseID)相等的情况下,存在有1条以上的成绩大于它的纪录

我觉得Select * from StuTable A
Where Exists(
select 1 from StuTable
Where CourseID=A.CourseID And Achievement>A.Achievement
Having Count(*)<=1
)
Order by CourseID,Achievement Desc 更好理解
lcooc 2005-05-30
  • 打赏
  • 举报
回复
不清楚,帮UP
rfq 2005-05-30
  • 打赏
  • 举报
回复
select * from students a where not exists(select * from students b where a.sno<>b.sno and b.sno>a.sno )
看看这个
mycolin 2005-05-30
  • 打赏
  • 举报
回复
分不够吗?再加20分

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧