sql:查询所有科目分数都在80分(含)以上的学生姓名

meteor57 2009-08-17 07:48:34
设tb表如下:
name subject grade

mike math 79
lee chinese 81
lee math 78
tom chinese 85
tom math 81
tom english 88
。。。。

查询所有科目分数都在80分(含)以上的学生姓名

哎,不会做....
特来请教~~~
先谢谢了~
...全文
2223 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
meteor57 2009-08-18
  • 打赏
  • 举报
回复
不好意思,是我没有说清楚,表中,每个人课程不一定相同,只要求所选的课程得分>80分就可以了.

[Quote=引用 8 楼 zc_0101 的回复:]
select name,min(grade) 最低分 from #T group by name having min(grade)>80
[/Quote]谢谢各位了!
这个貌似比较容易理解.
脑子老是转不过来,呵呵~^_^
feihong0233 2009-08-17
  • 打赏
  • 举报
回复
答zc_0101:
你可以试试你例子里
select 'mike','math',81 union all

你选择的是考试科目最低分也高于80的,
但是只有2科成绩或者1科成绩,也就是说缺考的都会被考虑,
这样应该违反搂主所有科目(个人理解是全科80以上的优等生)大于80……
youqi1984 2009-08-17
  • 打赏
  • 举报
回复
是的,SQL77比较全面
zc_0101 2009-08-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zc_0101 的回复:]
SQL code--========+++++++++++++++++++++++++++++++++++==========
--======= 每天都在进步,却依然追不上地球的自传=========
--======= By: zc_0101 At:2009-08-17 20:34:20=========
--========+++++++++++++++++++++++++++++++¡­
[/Quote]

这个最聪明
feihong0233 2009-08-17
  • 打赏
  • 举报
回复
虽然写成固定科目比较方便理解,不过SQL77的更全面
feihong0233 2009-08-17
  • 打赏
  • 举报
回复

/*如果科目是固定的话(当然,你这个看起来就只有三科,貌似小学成绩软件)
命题可以理解为计算成绩大于80分的科目数量为最大数,即3,由此可得*/
SELECT [Name] FROM [tb]
WHERE [Grade] >= 80
GROUP BY [Name] HAVING COUNT(*) = 3
ORDER BY [Name];
GO
zc_0101 2009-08-17
  • 打赏
  • 举报
回复

--========+++++++++++++++++++++++++++++++++++==========
--======= 每天都在进步,却依然追不上地球的自传=========
--======= By: zc_0101 At:2009-08-17 20:34:20=========
--========++++++++++++++++++++++++++++++++++++=========
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (name varchar(4),subject varchar(7),grade int)
insert into #T
select 'mike','math',79 union all
select 'lee','chinese',81 union all
select 'lee','math',78 union all
select 'tom','chinese',85 union all
select 'tom','math',81 union all
select 'tom','english',88

----------------查询------------
select name,min(grade) 最低分 from #T group by name having min(grade)>80
----------------结果--------------
/*
name 最低分
tom 81
*/

华夏小卒 2009-08-17
  • 打赏
  • 举报
回复

IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb( name varchar(10),subject varchar(10) ,grade int )
go
insert tb SELECT
'mike', 'math' ,79 UNION ALL SELECT
'lee' , 'chinese' ,81 UNION ALL SELECT
'lee' , 'math' ,78 UNION ALL SELECT
'tom' , 'chinese' ,85 UNION ALL SELECT
'tom' ,'math' ,81 UNION ALL SELECT
'tom' , 'english' ,88
go


SELECT DISTINCT NAME FROM TB T
WHERE NAME NOT IN(SELECT NAME FROM TB WHERE GRADE<80)

NAME
----------
tom

(1 行受影响)
guguda2008 2009-08-17
  • 打赏
  • 举报
回复
学习的时候都不自己写将来工作了怎么办?
SQL77 2009-08-17
  • 打赏
  • 举报
回复


IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb( name varchar(10),subject varchar(10) ,grade int )
go
insert tb SELECT
'mike', 'math' ,79 UNION ALL SELECT
'lee' , 'chinese' ,81 UNION ALL SELECT
'lee' , 'math' ,78 UNION ALL SELECT
'tom' , 'chinese' ,85 UNION ALL SELECT
'tom' ,'math' ,81 UNION ALL SELECT
'tom' , 'english' ,88
go

SELECT NAME FROM TB WHERE grade >=80 GROUP BY NAME
HAVING COUNT(*) =(SELECT COUNT(DISTINCT SUBJECT) FROM TB )

(所影响的行数为 6 行)

NAME
----------
tom

(所影响的行数为 1 行)
SQL77 2009-08-17
  • 打赏
  • 举报
回复


SELECT * FROM TB WHERE grade >=80
HAVING COUNT(*) =(SELECT COUNT(DISTINCT SUBJECT) FROM TB )
feixianxxx 2009-08-17
  • 打赏
  • 举报
回复
-- =========================================
-- -----------t_mac 小编-------------------
--------------------希望有天成为大虾----
-- =========================================

IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb( name varchar(10),subject varchar(10) ,grade int )
go
insert tb SELECT
'mike', 'math' ,79 UNION ALL SELECT
'lee' , 'chinese' ,81 UNION ALL SELECT
'lee' , 'math' ,78 UNION ALL SELECT
'tom' , 'chinese' ,85 UNION ALL SELECT
'tom' ,'math' ,81 UNION ALL SELECT
'tom' , 'english' ,88
go
select distinct name
from tb k
where not exists(select * from tb where k.name=name and grade<80)

go
name
----------
tom
sixer04 2009-08-17
  • 打赏
  • 举报
回复


select name from tb where name not in (select name from tb where grade<80)
feixianxxx 2009-08-17
  • 打赏
  • 举报
回复
select name
from tb k
where not exists(select * from tb where k.name=name and grade<80)

34,576

社区成员

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

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