表结构如下:

–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. AJAX - 向服务器发送请求请求

    AJAX - 向服务器发送请求请求 XMLHttpRequest 对象用于和服务器交换数据.直线电机生产厂家 向服务器发送请求 如需将请求发送到服务器,我们使用 XMLHttpRequest 对象的 ...

  2. 为什么C++中只有指针和引用才能实现多态?

    代码: class A { public: virtual void Debug(){} }; class B:public A { public: virtual void Debug(){} }; ...

  3. luoguP1774 最接近神的人_NOI导刊2010提高(02)x

    P1774 最接近神的人_NOI导刊2010提高(02) 题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门 ...

  4. Android如何安装系统应用,及自己增加安装系统应用的接口

    根据SIM卡安装系统应用 功能: 1:如何安装系统应用,apk放在system/app系统分区下面. 2:根据SIM卡的归属国家选择性的安装应用. 一:本人使用方法: 在开机的服务里面添加接口(Pac ...

  5. 关于SQL注入的问题以及解决方法

    1.关于SQL注入 什么是SQL注入: 由于jdbc程序在执行的过程中sql语句在拼装时使用了由页面传入参数,如果用户恶意传入一些sql中的特殊关键字,会导致sql语句意义发生变化,这种攻击方式就叫做 ...

  6. jdbcType="DATE"和jdbcType=" TIMESTAMP"的区别

    原文: https://www.cnblogs.com/fswhq/p/jdbcType.html 当传入null值时,jdbcType 会防止null空指针异常报错 Mybatis 中jdbcTyp ...

  7. webpack前置知识2(JavaScript项目初始化)

    所有的JavaScript项目都是在终端输入npm init -y进行项目初始化,如果要自定义项目规则,去掉 -y 参数. vscode终端快捷键ctrl+` 初始化 运行上述命令后,项目内会新建一个 ...

  8. [ros] ros入门记录

    ROS入门 半天入门ROS,总体感觉比较好理解,python写不用编译超级爽,学完ros去学电控去了. ros2比ros1好用,所以最终是学ros2. ros1 安装 添加源 > sudo sh ...

  9. easyhook源码分析二——注入

    EasyHook 中的注入方法. 函数原型 // EasyHook 中的命名比较有意思,Rh 代表的就是Remote Hook,此函数就是远程钩子的一个子过程----注入,前面的宏代表它是导出函数. ...

  10. leetcode434 字符串中的单词树(python)

    统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" ...