表结构如下:

–1.学生表

Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别

–2.课程表

Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号

–3.教师表

Teacher(t_id,t_name) –教师编号,教师姓名

–4.成绩表

Score(s_id,c_id,s_score) –学生编号,课程编号,分数

--建表SQL

--学生表

CREATE TABLE `Student`(

`s_id` VARCHAR(20),

`s_name` VARCHAR(20) NOT NULL DEFAULT '',

`s_birth` VARCHAR(20) NOT NULL DEFAULT '',

`s_sex` VARCHAR(10) NOT NULL DEFAULT '',

PRIMARY KEY(`s_id`)

);

--课程表

CREATE TABLE `Course`(

`c_id`  VARCHAR(20),

`c_name` VARCHAR(20) NOT NULL DEFAULT '',

`t_id` VARCHAR(20) NOT NULL,

PRIMARY KEY(`c_id`)

);

--教师表

CREATE TABLE `Teacher`(

`t_id` VARCHAR(20),

`t_name` VARCHAR(20) NOT NULL DEFAULT '',

PRIMARY KEY(`t_id`)

);

--成绩表

CREATE TABLE `Score`(

`s_id` VARCHAR(20),

`c_id`  VARCHAR(20),

`s_score` INT(3),

PRIMARY KEY(`s_id`,`c_id`)

);

--插入学生表测试数据

insert into Student values('01' , '赵雷' , '1990-01-01' , '男');

insert into Student values('02' , '钱电' , '1990-12-21' , '男');

insert into Student values('03' , '孙风' , '1990-05-20' , '男');

insert into Student values('04' , '李云' , '1990-08-06' , '男');

insert into Student values('05' , '周梅' , '1991-12-01' , '女');

insert into Student values('06' , '吴兰' , '1992-03-01' , '女');

insert into Student values('07' , '郑竹' , '1989-07-01' , '女');

insert into Student values('08' , '王菊' , '1990-01-20' , '女');

--课程表测试数据

insert into Course values('01' , '语文' , '02');

insert into Course values('02' , '数学' , '01');

insert into Course values('03' , '英语' , '03');

--教师表测试数据

insert into Teacher values('01' , '张三');

insert into Teacher values('02' , '李四');

insert into Teacher values('03' , '王五');

--成绩表测试数据

insert into Score values('01' , '01' , 80);

insert into Score values('01' , '02' , 90);

insert into Score values('01' , '03' , 99);

insert into Score values('02' , '01' , 70);

insert into Score values('02' , '02' , 60);

insert into Score values('02' , '03' , 80);

insert into Score values('03' , '01' , 80);

insert into Score values('03' , '02' , 80);

insert into Score values('03' , '03' , 80);

insert into Score values('04' , '01' , 50);

insert into Score values('04' , '02' , 30);

insert into Score values('04' , '03' , 20);

insert into Score values('05' , '01' , 76);

insert into Score values('05' , '02' , 87);

insert into Score values('06' , '01' , 31);

insert into Score values('06' , '03' , 34);

insert into Score values('07' , '02' , 89);

insert into Score values('07' , '03' , 98);

(一)评分标准:

  1. 学习每门课程的学生人数。(3分)

select COUNT(c_name),c_name from course join score on course.c_id=score .c_id join student on score.s_id=student.s_id

  1. 查询1990年出生的学生信息。(3分)

select * from student WHERE s_birth=1990

  1. 查询同名同姓且同性别的信息及各自的数量。(3分)

select COUNT(s_name&&s_sex),s_name,s_sex from student GROUP BY s_name,s_sex

  1. 查询名字中有‘风’字的学生信息。(3分)

select * from student WHERE s_name LIKE '%风'

  1. 查询出男生及女生的数量。(3分)

select COUNT(s_sex) from student GROUP BY s_sex='男',s_sex='女'

  1. 检索"01"课程分数小于60,按分数降序排列的学生信息。(5分)

select t2.* from score t1,student t2 where t2.s_id = t1.s_id and t1.s_score < '60' and t1.c_id = '01' order by t1.s_score;

  1. 查询"01"课程比"02"课程成绩低的学生的信息及课程分数。(5分)

select s1.*,s2.01_score,s2.02_score from student s1,

(select t1.s_id as s_id,t1.s_score

as 01_score,t2.s_score

as 02_score from score t1,score t2

where t1.s_id = t2.s_id

and t1.c_id = '01'

and t2.c_id = '02'

and t1.s_score > t2.s_score ) s2

where s1.s_id = s2.s_id;

  1. 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩。(5分)

select t1.s_id,t2.s_name,avg(t1.s_score) as avg_score from score t1

left join student t2 on t1.s_id = t2.s_id

group by t1.s_id

having avg(t1.s_score) >= 60

  1. 查询学过"张三"老师授课的同学的信息。(5分)

select * from student JOIN score on student.s_id=score.s_id

JOIN course on score.c_id=course.c_id

JOIN teacher on course.t_id=teacher.t_id

WHERE t_name='张三'

  1. 查询至少有一门没有考试的学生信息。(5分)

select s_score,s_name,c_name from score JOIN student on student.s_id=score.s_id

JOIN course on course.c_id=score.c_id

WHERE s_score=0

  1. 查询所有学生的出生日期及年龄。(5分)

select (YEAR(CURDATE())-YEAR(s_birth)) as age,s_name FROM student

  1. 查询半月过生日的学生信息。(5分)

select *,DATE_FORMAT(s_birth,"%d") as day from student as ab inner join score as bb on ab.s_id=bb.s_id join course as cb on bb.c_id=cb.c_id join teacher as db on cb.t_id=db.t_id having day < 15

  1. 绩。(10分)

select c_name,student.s_id,s_name,s_score from course JOIN score on course.c_id=score.c_id

JOIN student on student.s_id=score.s_id

WHERE s_score<60

  1. 查询各科成绩最高分、最低分和平均分:(10分)

以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

select

a.c_id,

b.c_name,

max(s_score),

min(s_score),

round(avg(s_score), 2),

round(100 *(sum(case when a.s_score >= 60 then 1 else 0 end)/ sum(case when a.s_score then 1 else 0 end)), 2) as 及格率,

round(100 *(sum(case when a.s_score >= 70 and a.s_score <= 80 then 1 else 0 end)/ sum(case when a.s_score then 1 else 0 end)), 2) as 中等率,

round(100 *(sum(case when a.s_score >= 80 and a.s_score <= 90 then 1 else 0 end)/ sum(case when a.s_score then 1 else 0 end)), 2) as 优良率,

round(100 *(sum(case when a.s_score >= 90 then 1 else 0 end)/ sum(case when a.s_score then 1 else 0 end)), 2) as 优秀率

from

score a

left join course b on

a.c_id = b.c_id

group by

a.c_id,

b.c_name

  1. 查询学生的总成绩并进行排名(高到低),显示排名数、姓名、性别、学号、总分。(10分)

select s_name,s_sex,student.s_id,SUM(s_score) from course JOIN score on course.c_id=score.c_id

JOIN student on score.s_id=student.s_id GROUP BY s_name

ORDER BY  SUM(s_score) DESC

  1. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比。(10分)

select course.c_name, course.c_id,

sum(case when score.s_score<=100 and score.s_score>85 then 1 else 0 end) as "[100-85]",

sum(case when score.s_score<=85 and score.s_score>70 then 1 else 0 end) as "[85-70]",

sum(case when score.s_score<=70 and score.s_score>60 then 1 else 0 end) as "[70-60]",

sum(case when score.s_score<=60 and score.s_score>0 then 1 else 0 end) as "[60-0]"

from score left join course

on score.c_id = course.c_id

group by score.c_id;

  1. 查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩。(10分)

select t5.*,t4.s_score as MAX_score,t4.c_id,t6.c_name

from (score t4,student t5)

right join course t6 on t6.c_id = t4.c_id

where t4.s_score = (select max(t3.s_score)

from teacher t1,course t2,score t3

where t1.t_name = '张三' and t1.t_id = t2.t_id and t2.c_id = t3.c_id)and t4.s_id = t5.s_id

多條件查詢SQL語句的更多相关文章

  1. PL/SQL Developer查詢已經執行過的SQL語句記錄 Ctrl+E

    PL/SQL Developer查詢已經執行過的SQL語句記錄 最近提数比较多,没有已存在的脚本信息,就手工写呀... 一次性打开了多个提数脚本文件,结果执行完后把脚本保存好了,但是最后的整理其它脚本 ...

  2. PreparedStatement執行sql語句

    import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org ...

  3. 如何查詢 SQL Server 資料庫中欄位值為 NULL 的資料(转)

    最近使用mssql的时候对于未null的字段查询不到 http://blogs.msdn.com/b/jchiou/archive/2008/05/01/sql-server-null.aspx 先建 ...

  4. PreparedStatement执行sql語句

    import com.loaderman.util.JdbcUtil; import java.sql.Connection; import java.sql.PreparedStatement; i ...

  5. spring boot 框架根據 sql 創建語句自動生成 MVC層類代碼

    GITHUB: https://github.com/lin1270/spring_boot_sql2code 會自動生成model.mapper.service.controller. 代碼使用No ...

  6. SQL 語法

    查詢 Sql = ("SELECT A1, A2, A5, A4 FROM Table1 ") 筆數 Sql = ("Select COUNT(*) From TW01. ...

  7. vue 條件語句

    條件判斷使用v-if.v-else-if.v-else. v-show

  8. 一次快速改寫 SQL Server 高效查詢的範例

    最近線上系統突然出現匯出資料超過 10 筆時,查詢逾時的狀況,在仔細查找之後. 發現了問題原因,透過應用端與數據端兩邊同時調整,將查詢的效率提昇了約數百倍以上 首先,原本應用端的商務邏輯為每一分頁筆數 ...

  9. 查看LINQ Expression編譯後的SQL語法(转)

    在用了LINQ語法之後的一個月,我幾乎把SQL語法全部拋到腦後了,不過 LINQ好用歸好用,但是實際上操作資料庫的還是SQL語法,如果不知道LINQ語法 編譯過後產生怎樣的SQL語法,一不小心效能就會 ...

随机推荐

  1. The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game

    题目:https://nanti.jisuanke.com/t/41420 思路:当a(a∈S′)为最小值 如果Sum(S′)−a≤Sum(S−S′)成立 那么(∀t∈S′,Sum(S′)−t≤Sum ...

  2. React native 之 async/await

    参考资料 :https://www.jianshu.com/p/1e75bd387aa0 要点: 1. async function(){} 将普通函数转换成Promise 2. await 表达式/ ...

  3. 编译caffe-gpu-cuda及cudnn-tar 下载地址

    y下载 https://github.com/BVLC/caffe https://github.com/BVLC/caffe/archive/master.zip gcc caffe安装 有2个问题 ...

  4. 原生JS获取li中的内容

  5. [BZOJ4305]数列的GCD:莫比乌斯反演+组合数学

    分析 一开始想的是对恰好\(k\)个位置容斥,结果发现对\(\gcd\)有些无从下手,想了想发现自己又sb了. 考虑对\(\gcd\)进行容斥处理,弱化条件,现在我们要求的是使\(\gcd\)是\(d ...

  6. wannafly 练习赛10 f 序列查询(莫队,分块预处理,链表存已有次数)

    链接:https://www.nowcoder.net/acm/contest/58/F 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524288K 64 ...

  7. 打开远程桌面时总提示无法打开连接文件default.rdp

    删除C:\Users\Administrator\Documents\default.rdp,再启动远程就好了 http://www.chahushequ.com/read-topic-94-2fa9 ...

  8. ES6字符串的拓展

    字符串的遍历接口 for...of循环遍历. for (let codePoint of 'foo') { console.log(codePoint) } // "f" // & ...

  9. Jmeter接口测试系列之测试用例变量参数化处理

    在进行接口测试时,一组完整的接口测试用例,存在后一个测试用例使用前一个用例的请求结果中的数据,此时就需要参数化测试用例中值.直接使用变量调用会存在问题,此时就需要用到beanshell去改变. 举例说 ...

  10. C# Console.WriteLine堵塞进程

    最近在项目中控制台为了调试使用Console.WriteLine(),发现在高并发的情况下会出现假锁状态,断点调试发现卡在Console.WriteLine那.需要进行一个键盘输入才可以继续. 关于C ...