数据库查询 查询“001”课程比“002”课程成绩高的所有学生的学号 怎么查询“001”课程比“002”课程成绩高的所有学生的学号...

作者&投稿:禤树 (若有异议请与网页底部的电邮联系)

分析如下:

--1select SC1.S#  from SC SC1 JOIN SC SC2 ON SC1.S#=SC2.S#

WHERE SC1.C#='001' AND SC2.C#='002' AND SC1.score>SC2.score

--2select S#,AVG(score)

平均成绩  from SC    group by S#    

having AVG(score)>60 --3select Student.S#,

Sname,COUNT(*) 选课数,SUM(score) 总成绩 

from Student JOIN SC on Student.S#=SC.S#  

group by Student.S#,Sname

扩展资料:

数据库操作的注意事项

1、对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。

2、应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t where num is null

最好不要给数据库留NULL,尽可能的使用 NOT NULL填充数据库.

备注、描述、评论之类的可以设置为 NULL,其他的,最好不要使用NULL。

不要以为 NULL 不需要空间,比如:char(100) 型,在字段建立时,空间就固定了, 不管是否插入值(NULL也包含在内),都是占用 100个字符的空间的,如果是varchar这样的变长字段, null 不占用空间。

可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:select id from t where num = 0

3、应尽量避免在 where 子句中使用 != 或 <> 操作符,否则将引擎放弃使用索引而进行全表扫描。

4、应尽量避免在 where 子句中使用 or 来连接条件,如果一个字段有索引,一个字段没有索引,将导致引擎放弃使用索引而进行全表扫描,如:select id from t where num=10 or Name = 'admin'

可以这样查询:
select id from t where num = 10
union all
select id from t where Name = 'admin'

5、in 和 not in 也要慎用,否则会导致全表扫描,如:select id from t where num in(1,2,3)

对于连续的数值,能用 between 就不要用 in 了:select id from t where num between 1 and 3

很多时候用 exists 代替 in 是一个好的选择:select num from a where num in(select num from b)

用下面的语句替换:select num from a where exists(select 1 from b where num=a.num)

6、下面的查询也将导致全表扫描:select id from t where name like ‘%abc%’

若要提高效率,可以考虑全文检索。

7、如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然 而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。

参考资料来源:百度百科:数据库



1、查询“001”课程比“002”课程成绩高的所有学生的学号:

SELECT * FROM tblScore ts1, tblScore ts2

WHERE ts1.stuid = ts2.stuid AND ts1.courseid = '001' AND ts2.courseid = '002

AND ts1.score > ts2.score

2、查询平均成绩大于60分的同学的学号和平均成绩:

SELECT stu.stuid, AVG(score.score)FROM tblstudent stu, tblscore score

WHERE stu.stuid = score.stuid GROUP BY stu.stuid

HAVING AVG(score.score) > 60

3、 查询所有同学的学号、姓名、选课数、总成绩:

SELECT stu.stuid, stu.stuname,COUNT(score.courseid),SUM(score.score)

FROM tblstudent stu, tblCourse course, tblScore score

WHERE stu.stuid = score.stuid AND course.courseid = score.courseid

GROUP BY stu.stuid,stu.stuname ORDER BY stuid

扩展资料

查询数据库使用SELECT命令。 这个命令相对比较复杂。可变化样式较多,这里分功能依次讲解。

1、查询数据表中所有数据 语法:select * from 表名。

2、查询指定字段的显示 语法:select 字段1,字段2,... from 表名。

3、as 别名 在查询时,默认结果显示的字段和表中字段名相同,可以通过别名来修改显示的样式 语法:select 字段1 as 别名,字段2 别名,... from 表名。在给字段起别名时,可以使用 as ,也可以直接在字段后跟别名,省略 as 。

4、消除重复数据 在查询数据时,查询结果可能会有很多重复的数据,如果不想重复,可以使用 distinct 来实现去重。 语法:select distinct 字段名 from 表名。distinct 在去重时,会比较所有的指定字段,只有完全相同时才认为是重复的。

5、带条件查询 where 子句 查询数据时,需要根据不同的需求设置条件。 通过 where 子句来设置查询条件。

参考资料

数据库-百度百科



用如下代码可以实现:

--1

select SC1.S#  from SC SC1 JOIN SC SC2 ON SC1.S#=SC2.S#    WHERE SC1.C#='001' AND SC2.C#='002' AND SC1.score>SC2.score --2select S#,AVG(score) 平均成绩  from SC    group by S#      having AVG(score)>60 --3select Student.S#,Sname,COUNT(*) 选课数,SUM(score) 总成绩   from Student JOIN SC on Student.S#=SC.S#      group by Student.S#,Sname

拓展资料:

以下为在MySQL数据库中查询数据通用的 SELECT 语法:

SELECT column_name,column_name
FROM table_name[WHERE Clause][LIMIT N][ OFFSET M]

  • 查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。

  • SELECT 命令可以读取一条或者多条记录。

  • 你可以使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据

  • 你可以使用 WHERE 语句来包含任何条件。

  • 你可以使用 LIMIT 属性来设定返回的记录数。

  • 你可以通过OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0。



--1
select SC1.S#
  from SC SC1 JOIN SC SC2 ON SC1.S#=SC2.S#
    WHERE SC1.C#='001' AND SC2.C#='002' AND SC1.score>SC2.score

--2
select S#,AVG(score) 平均成绩
  from SC
    group by S#
  having AVG(score)>60

--3
select Student.S#,Sname,COUNT(*) 选课数,SUM(score) 总成绩
   from Student JOIN SC on Student.S#=SC.S#
      group by Student.S#,Sname


怎么查询“001”课程比“002”课程成绩高的所有学生的学号?表结构如下:~

select a.sid, a.score as score1,b.score as score2 from (select * from scoretable where cid='001') a
left join
(select * from scoretable where cid='002')b
on a.sid=b.sid
where a.score>b.score

你可以登陆学校网站输入学籍编号查询,或则致电他们的班主任,祝你快乐幸福!
一般还是不要再网上来问吧,问不到什么东西的。