欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

多條件查詢SQL語句

程序员文章站 2022-03-20 17:15:12
表结构如下: –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号 –3.教师表 Teacher(t_id,t_name) ......

表结构如下:

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